ASP MVC route with parameter in the begin of url - asp.net

Hello by default ASP MVC routes are looks like:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
but is it possible to use it in next way with not optional ID:
routes.MapRoute(
name: "Default",
url: "{id}/{controller}/{action}",
defaults: new { controller = "Home", action = "Index", id = 0 }
);

Yes, it is possible.
Just tried:
RoutesRegistration:
routes.MapRoute(
"Default",
"{id}/{controller}/{action}",
new { controller = "Home", action = "Index", id = 0}
);
Controller:
public class HomeController : Controller
{
public ActionResult Index(int id)
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
}

Related

Routing deletes "/Index" from URL

in my application I have a CompanyGroupDataController.cs.
When the execution gets into its main action (Index), through this URL:.../CompanyGroupData/Index, the URL not longer ends in "/Index", so it erases the last part (althoug in the browser bar it keeps it). I think the problem is in my RouteConfig.cs which is this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{company}/{controller}/{action}/{id}",
defaults: new
{
company = Skill4IT.WebApps.Admin.Helpers.Tools.AdminUrlAccessCode,
controller = "Home",
action = "Index",
id = UrlParameter.Optional
},
namespaces: new string[] { "Skill4IT.WebApps.Admin.Controllers" }
);
routes.MapRoute(
name: "spa",
url: "#{controller}/{action}/{id}",
defaults: new
{
company = "admin",
controller = "Home",
action = "Index",
id = UrlParameter.Optional
},
namespaces: new string[] { "Skill4IT.WebApps.Admin.Controllers" }
);
routes.MapRoute(
name: "Catchall",
url: "{*catchall}",
defaults: new
{
controller = "Error",
action = "Error404",
}
);
Thanks

How to pass string as parameter in ASP.Net MVC

I am trying to make a simple sample of passing string parameter in URL not using the queryString at this example. First of all I added a new MapRouteto RouteConfig.cs file as
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "app",
url: "{controller}/{action}/{name}"
);
and in Controller I have
public class AppController : Controller
{
public string Index(string name)
{
return HttpUtility.HtmlEncode("Hello " + name );
}
}
but the view is not displaying the string parameter. For example a URL like this http://localhost:59013/App/Index/Ali only return Hello!
Why is this happening?
Update
routes.MapRoute(
name: "app",
url: "{controller}/{action}/{name}",
defaults: new {action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
First, you need to change the order of your registered routes so the default will be the last.
Secondly, your app route pattern is wrong and will always collide with the default route. You better change its pattern to
url: "App/Index/{name}"
Or perhaps to the more friendlier
url: "App/{name}"
Both with
defaults: new { controller = "App", action = "Index" }
So that your routes would look like:
routes.MapRoute(
name: "app",
url: "App/{name}",
defaults: new { controller = "App", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
See MSDN

Azure MVC Angular Project not finding Home page

Locally the website works fine, when deployed to azure we get the view index or its master was not found or no view engine supports the searched locations.
Routeconfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "websitename",
url: "",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "{controller}", action = "{action}", id = UrlParameter.Optional }
);
We are using angularjs and having no problems until deployed to azure other than this issue.
You define following for home page right?
routes.MapRoute(
name: "DoransData",
url: "",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Change it to
routes.MapRoute(
name: "DoransData",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Based on your code I think that you could only need following code
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

Rename the Controller Name in the URL

I have 2 brands which use the same controller. In one brand the URL must look like ~Home/Index and in other URL should look like ~Account/Index, but the both URLs must point to the same Home Controller action methods.
Please give any idea how to achieve this.
Specify 2 route in the RouteConfig.cs
routes.MapRoute(
name: "Default",
url: "Home/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default1",
url: "Account/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Use this URL to call the Index() method in Home controller
1) http://localhost/../Home/Index
2) http://localhost/../Account/Index
Put below code inside Global.asax.cs
routes.MapRoute(
name: "Account",
url: "Account/Index",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "Home/Index",
defaults: new { controller = "Home", action = "Index" }
);
*Note:Always Default name put in last line in golbal.asax.cs

ASP.NET WebPages Routing

I created simple asp.net project.
Have folder Account, and many files like Register.cshtml, LogIn, etc...
And url: localhost/Account/Register
Want it to be: localhost/Register, localhost/LogIn.
Tried to map it in _AppStart.cshtml:
RouteTable.Routes.MapPageRoute("Account", "{Action}", "~/Account/{Action}");
And it doesn't work.
First of all you have to use MapRoute to MapPageRoute, because MapPageRoute is (from msdn):
Provides a way o define routes for Web Forms applications
But if you modify your code like this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("AccountRoute", "{Action}", new {controller = "Account"});
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Every request without parameters will go to AccountRoute, even if it should be handled by other controler.
The only way it will be working is to create code like this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//ALL routes you need
routes.MapRoute("LoginRoute", "{Action}", new { controller = "Account", action = "Login" });
routes.MapRoute("RegisterRoute", "{Action}", new { controller = "Account", action = "Register" });
//Default route in the end
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Try this one....
//Default
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"",
new { controller = "Home", action = "Index", id = "" }
);
//url rewriting
RouteTable.Routes.MapRoute(null, "Register/{ID}", new { controller = "Home", action = "Register" });

Resources