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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Named Routes with ASP.NET MVC

Dane Morgridge user avatar by
Dane Morgridge
·
Aug. 13, 10 · · Interview
Like (0)
Save
Tweet
16.60K 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

  • Design Patterns for Microservices
  • 10 Programming Habits a Web Developer Should Embrace
  • How to Submit a Post to DZone
  • 12 Modern CSS Techniques For Older CSS Problems

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo