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

PPTX
Build your web app with asp.net mvc 2 from scratch
PPTX
Being a pimp without silverlight
PPSX
Baking With Cake Php
ODP
Compress
PDF
Connecting To MS SQL Server With Mulesoft (Stored Procedure To Insert data)
PPTX
Being a pimp without silverlight
PPTX
COMPLETE Cloud – Capgemini’s Business Platform – Powered by AWS/SUSE
PPT
SAP Demo
Build your web app with asp.net mvc 2 from scratch
Being a pimp without silverlight
Baking With Cake Php
Compress
Connecting To MS SQL Server With Mulesoft (Stored Procedure To Insert data)
Being a pimp without silverlight
COMPLETE Cloud – Capgemini’s Business Platform – Powered by AWS/SUSE
SAP Demo

Viewers also liked (7)

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

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

PPTX
express.js.pptxgghhhhhhnnbvcdssazxvuyiknvc
PPT
Integrate Sas With Google Maps
PPT
ASP.NET MVC introduction
PPS
Introduction To Mvc
PDF
AngularJS 101 - Everything you need to know to get started
PDF
AngularJS Basic Training
PPTX
Http and REST APIs.
PDF
Introducing Rendr: Run your Backbone.js apps on the client and server
PPTX
AngularJS Fundamentals + WebAPI
PPTX
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
PPTX
Chapter5.pptx
PPTX
Fundaments of Knockout js
PPTX
Asp.Net Mvc
PPTX
Mvc3 part2
PPTX
Spring mvc
PDF
Express node js
PDF
Camunda BPM 7.2: Tasklist and Javascript Forms SDK (English)
DOCX
Angular js
PPTX
Getting into ember.js
PDF
Infosys Angular Interview Questions PDF By ScholarHat
express.js.pptxgghhhhhhnnbvcdssazxvuyiknvc
Integrate Sas With Google Maps
ASP.NET MVC introduction
Introduction To Mvc
AngularJS 101 - Everything you need to know to get started
AngularJS Basic Training
Http and REST APIs.
Introducing Rendr: Run your Backbone.js apps on the client and server
AngularJS Fundamentals + WebAPI
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
Chapter5.pptx
Fundaments of Knockout js
Asp.Net Mvc
Mvc3 part2
Spring mvc
Express node js
Camunda BPM 7.2: Tasklist and Javascript Forms SDK (English)
Angular js
Getting into ember.js
Infosys Angular Interview Questions PDF By ScholarHat
Ad

Recently uploaded (20)

PDF
Enhancing plagiarism detection using data pre-processing and machine learning...
PPTX
Microsoft Excel 365/2024 Beginner's training
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
Architecture types and enterprise applications.pdf
PDF
Getting started with AI Agents and Multi-Agent Systems
PPT
Geologic Time for studying geology for geologist
PPTX
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PDF
CloudStack 4.21: First Look Webinar slides
PDF
A proposed approach for plagiarism detection in Myanmar Unicode text
PDF
The influence of sentiment analysis in enhancing early warning system model f...
DOCX
Basics of Cloud Computing - Cloud Ecosystem
PDF
A review of recent deep learning applications in wood surface defect identifi...
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PDF
Five Habits of High-Impact Board Members
PDF
“A New Era of 3D Sensing: Transforming Industries and Creating Opportunities,...
PPT
Module 1.ppt Iot fundamentals and Architecture
PDF
Developing a website for English-speaking practice to English as a foreign la...
PDF
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
Enhancing plagiarism detection using data pre-processing and machine learning...
Microsoft Excel 365/2024 Beginner's training
Zenith AI: Advanced Artificial Intelligence
Architecture types and enterprise applications.pdf
Getting started with AI Agents and Multi-Agent Systems
Geologic Time for studying geology for geologist
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
Final SEM Unit 1 for mit wpu at pune .pptx
CloudStack 4.21: First Look Webinar slides
A proposed approach for plagiarism detection in Myanmar Unicode text
The influence of sentiment analysis in enhancing early warning system model f...
Basics of Cloud Computing - Cloud Ecosystem
A review of recent deep learning applications in wood surface defect identifi...
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
Five Habits of High-Impact Board Members
“A New Era of 3D Sensing: Transforming Industries and Creating Opportunities,...
Module 1.ppt Iot fundamentals and Architecture
Developing a website for English-speaking practice to English as a foreign la...
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
How ambidextrous entrepreneurial leaders react to the artificial intelligence...

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