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

Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 

Recently uploaded (20)

Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 

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