SlideShare a Scribd company logo
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 :)
Ad

Recommended

Build your web app with asp.net mvc 2 from scratch
Build your web app with asp.net mvc 2 from scratch
Chalermpon Areepong
 
Being a pimp without silverlight
Being a pimp without silverlight
Kris van der Mast
 
Baking With Cake Php
Baking With Cake Php
valberg
 
Compress
Compress
cziltang
 
Connecting To MS SQL Server With Mulesoft (Stored Procedure To Insert data)
Connecting To MS SQL Server With Mulesoft (Stored Procedure To Insert data)
Jitendra Bafna
 
Being a pimp without silverlight
Being a pimp without silverlight
Maarten Balliauw
 
COMPLETE Cloud – Capgemini’s Business Platform – Powered by AWS/SUSE
COMPLETE Cloud – Capgemini’s Business Platform – Powered by AWS/SUSE
Dirk Oppenkowski
 
SAP Demo
SAP Demo
syscomsoftwares
 
22827361 ab initio-fa-qs
22827361 ab initio-fa-qs
Capgemini
 
66141293 sap-sd-cin
66141293 sap-sd-cin
Goutam Ghosh
 
Pricing Concept in SAP SD by Venkat Mannam
Pricing Concept in SAP SD by Venkat Mannam
Venkat Mannam
 
66141912 sap-sd-advanced-pricing
66141912 sap-sd-advanced-pricing
vishnuj02
 
SAP SD Study material
SAP SD Study material
Harsha Halyal
 
SAP SD Variant configuration-training-document 2
SAP SD Variant configuration-training-document 2
ramesh 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...
Capgemini
 
express.js.pptxgghhhhhhnnbvcdssazxvuyiknvc
express.js.pptxgghhhhhhnnbvcdssazxvuyiknvc
rani marri
 
Integrate Sas With Google Maps
Integrate Sas With Google Maps
vineetkaul
 
ASP.NET MVC introduction
ASP.NET MVC introduction
Tomi Juhola
 
Introduction To Mvc
Introduction To Mvc
Volkan Uzun
 
AngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get started
Stéphane Bégaudeau
 
AngularJS Basic Training
AngularJS Basic Training
Cornel Stefanache
 
Http and REST APIs.
Http and REST APIs.
Rahul Tanwani
 
Introducing Rendr: Run your Backbone.js apps on the client and server
Introducing Rendr: Run your Backbone.js apps on the client and server
Spike Brehm
 
AngularJS Fundamentals + WebAPI
AngularJS Fundamentals + WebAPI
Eric Wise
 
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
Learnimtactics
 
Chapter5.pptx
Chapter5.pptx
narendrakumar406336
 
Fundaments of Knockout js
Fundaments of Knockout js
Flavius-Radu Demian
 
Asp.Net Mvc
Asp.Net Mvc
micham
 
Mvc3 part2
Mvc3 part2
Muhammad Younis
 
Spring mvc
Spring mvc
Harshit Choudhary
 

More Related Content

Viewers also liked (7)

22827361 ab initio-fa-qs
22827361 ab initio-fa-qs
Capgemini
 
66141293 sap-sd-cin
66141293 sap-sd-cin
Goutam Ghosh
 
Pricing Concept in SAP SD by Venkat Mannam
Pricing Concept in SAP SD by Venkat Mannam
Venkat Mannam
 
66141912 sap-sd-advanced-pricing
66141912 sap-sd-advanced-pricing
vishnuj02
 
SAP SD Study material
SAP SD Study material
Harsha Halyal
 
SAP SD Variant configuration-training-document 2
SAP SD Variant configuration-training-document 2
ramesh 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...
Capgemini
 
22827361 ab initio-fa-qs
22827361 ab initio-fa-qs
Capgemini
 
66141293 sap-sd-cin
66141293 sap-sd-cin
Goutam Ghosh
 
Pricing Concept in SAP SD by Venkat Mannam
Pricing Concept in SAP SD by Venkat Mannam
Venkat Mannam
 
66141912 sap-sd-advanced-pricing
66141912 sap-sd-advanced-pricing
vishnuj02
 
SAP SD Study material
SAP SD Study material
Harsha Halyal
 
SAP SD Variant configuration-training-document 2
SAP SD Variant configuration-training-document 2
ramesh 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...
Capgemini
 

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

express.js.pptxgghhhhhhnnbvcdssazxvuyiknvc
express.js.pptxgghhhhhhnnbvcdssazxvuyiknvc
rani marri
 
Integrate Sas With Google Maps
Integrate Sas With Google Maps
vineetkaul
 
ASP.NET MVC introduction
ASP.NET MVC introduction
Tomi Juhola
 
Introduction To Mvc
Introduction To Mvc
Volkan Uzun
 
AngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get started
Stéphane Bégaudeau
 
AngularJS Basic Training
AngularJS Basic Training
Cornel Stefanache
 
Http and REST APIs.
Http and REST APIs.
Rahul Tanwani
 
Introducing Rendr: Run your Backbone.js apps on the client and server
Introducing Rendr: Run your Backbone.js apps on the client and server
Spike Brehm
 
AngularJS Fundamentals + WebAPI
AngularJS Fundamentals + WebAPI
Eric Wise
 
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
Learnimtactics
 
Chapter5.pptx
Chapter5.pptx
narendrakumar406336
 
Fundaments of Knockout js
Fundaments of Knockout js
Flavius-Radu Demian
 
Asp.Net Mvc
Asp.Net Mvc
micham
 
Mvc3 part2
Mvc3 part2
Muhammad Younis
 
Spring mvc
Spring mvc
Harshit Choudhary
 
Express node js
Express node js
Yashprit Singh
 
Camunda BPM 7.2: Tasklist and Javascript Forms SDK (English)
Camunda BPM 7.2: Tasklist and Javascript Forms SDK (English)
camunda services GmbH
 
Angular js
Angular js
prasaddammalapati
 
Getting into ember.js
Getting into ember.js
reybango
 
Infosys Angular Interview Questions PDF By ScholarHat
Infosys Angular Interview Questions PDF By ScholarHat
Scholarhat
 
express.js.pptxgghhhhhhnnbvcdssazxvuyiknvc
express.js.pptxgghhhhhhnnbvcdssazxvuyiknvc
rani marri
 
Integrate Sas With Google Maps
Integrate Sas With Google Maps
vineetkaul
 
ASP.NET MVC introduction
ASP.NET MVC introduction
Tomi Juhola
 
Introduction To Mvc
Introduction To Mvc
Volkan Uzun
 
AngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get started
Sté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 server
Spike Brehm
 
AngularJS Fundamentals + WebAPI
AngularJS Fundamentals + WebAPI
Eric Wise
 
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
Learnimtactics
 
Asp.Net Mvc
Asp.Net Mvc
micham
 
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.js
reybango
 
Infosys Angular Interview Questions PDF By ScholarHat
Infosys Angular Interview Questions PDF By ScholarHat
Scholarhat
 
Ad

Recently uploaded (20)

OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
 
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
Information Security Response Team Nepal_npCERT_Vice_President_Sudan_Jha.pdf
Information Security Response Team Nepal_npCERT_Vice_President_Sudan_Jha.pdf
ICT Frame Magazine Pvt. Ltd.
 
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
Edge AI and Vision Alliance
 
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 
OWASP Barcelona 2025 Threat Model Library
OWASP Barcelona 2025 Threat Model Library
PetraVukmirovic
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
 
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
Information Security Response Team Nepal_npCERT_Vice_President_Sudan_Jha.pdf
Information Security Response Team Nepal_npCERT_Vice_President_Sudan_Jha.pdf
ICT Frame Magazine Pvt. Ltd.
 
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
Edge AI and Vision Alliance
 
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 
OWASP Barcelona 2025 Threat Model Library
OWASP Barcelona 2025 Threat Model Library
PetraVukmirovic
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
Ad

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