DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. Named Routes with ASP.NET MVC

Named Routes with ASP.NET MVC

Dane Morgridge user avatar by
Dane Morgridge
·
Aug. 13, 10 · Interview
Like (0)
Save
Tweet
Share
16.91K Views

Join the DZone community and get the full member experience.

Join For Free
The routing engine in ASP.NET MVC is very powerful and one feature of them is the ability to use named routes.  They can help when you need to get the url of a route so that it can be accessed.  In the article on routing I posted a few weeks ago, I go over the basics and in your Global.asax.cs file you can setup your routes.  The default one looks like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
If you notice, the first parameter of the MapRoute method is the route name.  Here it is set to "Default" but it could just be null if we wanted.  The name on this route doesn't really do us much good. It doesn't help us much because we still have to specify the controller and action when building an action link or using some other method to get the URL.

If I use the privacy policy example I can create a link in code by using:
<%: Html.ActionLink("Privacy Policy", "Privacy", "Home")%>
Here the ActionLink method takes three parameters: link text, action and controller in that order. This will used the default route mechanism and we will have a URL that is /Home/Privacy.  This is of course not necessarily SEO optimized so we may have a route just for the privacy policy like:
routes.MapRoute(
"Privacy",
"privacy",
new { controller = "Home", action = "Privacy" }
);
In this route, the name is "Privacy", the URL will be /privacy and it will use the Home controller and Privacy action method.  I showed how you can use URL extensions to help with this, but you can also use a named route if you want to use an action link.  When using a named route, you simply create the action link with the name of the route instead of specifying the controller and action:
<%: Html.ActionLink("Privacy Policy", "Privacy") %>
The HTML link that will get created will look like this:
<a href="/privacy">Privacy Policy</a> 
As you can see it can make working with routes a bit easier.  It works very well when you have several routes that are named and you don't want to have to worry about setting the controller and action.  If you are using more dynamic routes like the default one, you may not want to use named routes and you will have to do more configuration in the action link. As with everything in development, it depends on what you are doing.  Named routes can certainly help you when working with ASP.NET MVC, but they won't be the answer every time.
ASP.NET MVC ASP.NET

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Integrating AWS Secrets Manager With Spring Boot
  • Kubernetes-Native Development With Quarkus and Eclipse JKube
  • Beyond Coding: The 5 Must-Have Skills to Have If You Want to Become a Senior Programmer
  • How To Perform Local Website Testing Using Selenium And Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: