Routing allows for easily readable URLs in ASP.NET MVC applications. As applications grow larger, URLs can become complex and difficult for users and developers to understand. Custom routing allows developers to define URL patterns and constraints to control how URLs are generated. The routing engine matches incoming URL patterns to routing rules defined in the application and determines which controller and action to execute. Developers can define multiple route rules from most specific to default to ensure the desired actions are executed.
1 of 3
Download to read offline
More Related Content
Custom routing in asp.net mvc
1. Custom Routing in ASP.Net MVC
Routing is one of the most important feature of ASP.Net MVC. It allows to create easily
readable and understandable urls. We do not face any problem when we work with
small applications where url is limited in size and format but as our application grows,
url sometimes need to generate under some guidelines or plans. Large applications
usually have complex urls that are hard to understand for end users and even for
developers too.
Here I am not going to explain all the things but try to cover some basic and important
things.
How request executes in ASP.Net MVC
Whenever request comes from browser or client side. It hits the controller, appropriate
action is recognized, business logic gets executed, model is bound and response is
return. That's how view is rendered at client side in ASP.Net MVC.
Default Route in MVC
http://domain-name/{controller}/{action}/{id}
Syntax
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
new { constraint },
new[] { namespace }
);
Example: domain-name/Home
route name: in the MapRoute should always be unique.
url: that you want user to actually see at client side or browser.
defaults: defines controller and action url should invoke. id is optional if it we do not
pass it then it will take default value if defined.
2. We can add restriction that url should obey if not request will not execute.
We can add the namespace to the route too. But when we need this, let me explain for
example. If we have two controller with same name with in different areas. Routing
engine will confuse whom to proceed with. In such situation we can define the
namespace that will solve such problem.
We can define multiple parameters in route. All parameters will have default values.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{year}/{month}/{day}",
defaults: new { controller = "Home", action = "Index", year=2000, month=01,
day=01}
new { constraint },
new[] { namespace }
);
constraint can be for parameter validation using regular expression or action specific
too.
new {year = @"d+", month= @"d+", day = @"d+"}
Example: domain-name/Home/2014/04/24
Above constraint would not allow url without numbers.
Routing engine match url pattern with routing list if matches it does not go for next
route rule. Therefore sometimes action we desire does not execute as route rule meets
before itself. In such cases, We can add action specific constraint as well.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{year}/{month}/{day}",
defaults: new { controller = "Home", action = "GetDetails", year=2000, month=01,
day=01}
new { GetDetails|GetDetailsYearWise},
new[] { namespace }
);
3. Here above url pattern will execute only if action name is GetDetails or
GetDetailsYearWise
Note: Our route rules should start with most specific to default. Most specific should
come first and less specific finally write default route rule in the last.
Default route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
When application runs, route engine automatically convert urls for button and link if
route rule matches the url pattern.
In ASP.Net MVC, @Html.RouteLink can be used to redirect as per route rule.
Example
@Html.RouteLink("Link name", "route-name", new { Controller ="controller name"
action = "action name", {parameter if any})
This is how route engine provides the full control over the url in asp.net mvc.
Happy Coding :)