Custom Routing in ASP.Net MVC
Routing is one of the most important feature of ASP.Net MVC. It allows to create easily
read...
We can add restriction that url should obey if not request will not execute.
We can add the namespace to the route too. Bu...
Here above url pattern will execute only if action name is GetDetails or
GetDetailsYearWise
Note: Our route rules should s...
Upcoming SlideShare
Loading in …5
×

Custom routing in asp.net mvc

642 views
500 views

Published on

Basic and important about custom routing in asp.net MVC

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
642
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Custom routing in asp.net mvc

  1. 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. 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. 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 :)

×