can we have multiple routing patterns in MVC something like below
example.
url: "{controller}/{action}/{id}" ---> Standard way;
url: "{action}/{controller}/{id}" ---> Is this the valid routing pattern
- Both routing patterns that you specified in your question are valid.
You can easily test it by adding some custom routes to your route config. For example by adding route:
routes.MapRoute(
name: "test",
url: "{action}/{controller}/{id}");
and typing http://localhost/TestAction/Home/8 in your browser you will hit the following action:
public class HomeController : Controller
{
public ActionResult TestAction(int id)
{
return Json(id,JsonRequestBehavior.AllowGet);
}
}
How does routing engine identifies which segment is actionname and
which segment is controllername?
Routing engine identifies action and controller segments by {action} and {controller} route parameters or by default values that you supply when you register route.
It is also easily testable: Remove default values from your route and change url to {notcontroller}/{action}/{id} you will get System.InvalidOperationException: The matched route does not include a 'controller' route value, which is required.
MVC will map a URL to the first route that fits the route pattern so route declaration order is extremely important - you should declare the most constrained routes first and the default route should be the last one.
In general mvc routing is very flexible. You can create different url patterns for example:
Routes.MapRoute(
name: "test",
url: "{controller}-{action}/{id}");
Will map requests such as http://localhost/Home-Index/5
MVC 5 introduced Attribute routing which provides even greater flexibility.