SlideShare a Scribd company logo
1 of 3
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.
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 }
);
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 :)

More Related Content

Viewers also liked

22827361 ab initio-fa-qs
22827361 ab initio-fa-qs22827361 ab initio-fa-qs
22827361 ab initio-fa-qsCapgemini
 
66141293 sap-sd-cin
66141293 sap-sd-cin66141293 sap-sd-cin
66141293 sap-sd-cinGoutam Ghosh
 
Pricing Concept in SAP SD by Venkat Mannam
Pricing Concept in SAP SD by Venkat MannamPricing Concept in SAP SD by Venkat Mannam
Pricing Concept in SAP SD by Venkat MannamVenkat Mannam
 
66141912 sap-sd-advanced-pricing
66141912 sap-sd-advanced-pricing66141912 sap-sd-advanced-pricing
66141912 sap-sd-advanced-pricingvishnuj02
 
SAP SD Study material
SAP SD Study material SAP SD Study material
SAP SD Study material Harsha Halyal
 
SAP SD Variant configuration-training-document 2
SAP SD Variant configuration-training-document 2SAP SD Variant configuration-training-document 2
SAP SD Variant configuration-training-document 2ramesh Charantimath
 
The Currency of Trust: Why Banks and Insurers Must Make Customer Data Safer a...
The Currency of Trust: Why Banks and Insurers Must Make Customer Data Safer a...The Currency of Trust: Why Banks and Insurers Must Make Customer Data Safer a...
The Currency of Trust: Why Banks and Insurers Must Make Customer Data Safer a...Capgemini
 

Viewers also liked (7)

22827361 ab initio-fa-qs
22827361 ab initio-fa-qs22827361 ab initio-fa-qs
22827361 ab initio-fa-qs
 
66141293 sap-sd-cin
66141293 sap-sd-cin66141293 sap-sd-cin
66141293 sap-sd-cin
 
Pricing Concept in SAP SD by Venkat Mannam
Pricing Concept in SAP SD by Venkat MannamPricing Concept in SAP SD by Venkat Mannam
Pricing Concept in SAP SD by Venkat Mannam
 
66141912 sap-sd-advanced-pricing
66141912 sap-sd-advanced-pricing66141912 sap-sd-advanced-pricing
66141912 sap-sd-advanced-pricing
 
SAP SD Study material
SAP SD Study material SAP SD Study material
SAP SD Study material
 
SAP SD Variant configuration-training-document 2
SAP SD Variant configuration-training-document 2SAP SD Variant configuration-training-document 2
SAP SD Variant configuration-training-document 2
 
The Currency of Trust: Why Banks and Insurers Must Make Customer Data Safer a...
The Currency of Trust: Why Banks and Insurers Must Make Customer Data Safer a...The Currency of Trust: Why Banks and Insurers Must Make Customer Data Safer a...
The Currency of Trust: Why Banks and Insurers Must Make Customer Data Safer a...
 

Similar to Custom routing in asp.net mvc

Integrate Sas With Google Maps
Integrate Sas With Google MapsIntegrate Sas With Google Maps
Integrate Sas With Google Mapsvineetkaul
 
ASP.NET MVC introduction
ASP.NET MVC introductionASP.NET MVC introduction
ASP.NET MVC introductionTomi Juhola
 
Introduction To Mvc
Introduction To MvcIntroduction To Mvc
Introduction To MvcVolkan Uzun
 
AngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedAngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedStéphane Bégaudeau
 
Introducing Rendr: Run your Backbone.js apps on the client and server
Introducing Rendr: Run your Backbone.js apps on the client and serverIntroducing Rendr: Run your Backbone.js apps on the client and server
Introducing Rendr: Run your Backbone.js apps on the client and serverSpike Brehm
 
AngularJS Fundamentals + WebAPI
AngularJS Fundamentals + WebAPIAngularJS Fundamentals + WebAPI
AngularJS Fundamentals + WebAPIEric Wise
 
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle StudiosAngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle StudiosLearnimtactics
 
Asp.Net Mvc
Asp.Net MvcAsp.Net Mvc
Asp.Net Mvcmicham
 
Camunda BPM 7.2: Tasklist and Javascript Forms SDK (English)
Camunda BPM 7.2: Tasklist and Javascript Forms SDK (English)Camunda BPM 7.2: Tasklist and Javascript Forms SDK (English)
Camunda BPM 7.2: Tasklist and Javascript Forms SDK (English)camunda services GmbH
 
Getting into ember.js
Getting into ember.jsGetting into ember.js
Getting into ember.jsreybango
 
Server side programming bt0083
Server side programming bt0083Server side programming bt0083
Server side programming bt0083Divyam Pateriya
 

Similar to Custom routing in asp.net mvc (20)

Integrate Sas With Google Maps
Integrate Sas With Google MapsIntegrate Sas With Google Maps
Integrate Sas With Google Maps
 
ASP.NET MVC introduction
ASP.NET MVC introductionASP.NET MVC introduction
ASP.NET MVC introduction
 
Introduction To Mvc
Introduction To MvcIntroduction To Mvc
Introduction To Mvc
 
AngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedAngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get started
 
AngularJS Basic Training
AngularJS Basic TrainingAngularJS Basic Training
AngularJS Basic Training
 
Http and REST APIs.
Http and REST APIs.Http and REST APIs.
Http and REST APIs.
 
Introducing Rendr: Run your Backbone.js apps on the client and server
Introducing Rendr: Run your Backbone.js apps on the client and serverIntroducing Rendr: Run your Backbone.js apps on the client and server
Introducing Rendr: Run your Backbone.js apps on the client and server
 
AngularJS Fundamentals + WebAPI
AngularJS Fundamentals + WebAPIAngularJS Fundamentals + WebAPI
AngularJS Fundamentals + WebAPI
 
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle StudiosAngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
 
Chapter5.pptx
Chapter5.pptxChapter5.pptx
Chapter5.pptx
 
Fundaments of Knockout js
Fundaments of Knockout jsFundaments of Knockout js
Fundaments of Knockout js
 
Asp.Net Mvc
Asp.Net MvcAsp.Net Mvc
Asp.Net Mvc
 
Mvc3 part2
Mvc3   part2Mvc3   part2
Mvc3 part2
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
Express node js
Express node jsExpress node js
Express node js
 
Camunda BPM 7.2: Tasklist and Javascript Forms SDK (English)
Camunda BPM 7.2: Tasklist and Javascript Forms SDK (English)Camunda BPM 7.2: Tasklist and Javascript Forms SDK (English)
Camunda BPM 7.2: Tasklist and Javascript Forms SDK (English)
 
Angular js
Angular jsAngular js
Angular js
 
Getting into ember.js
Getting into ember.jsGetting into ember.js
Getting into ember.js
 
Mvc
MvcMvc
Mvc
 
Server side programming bt0083
Server side programming bt0083Server side programming bt0083
Server side programming bt0083
 

Recently uploaded

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 

Recently uploaded (20)

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 

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