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 >

ASP.NET MVC - Routing

Dave Bush user avatar by
Dave Bush
·
Mar. 26, 09 · · News
Like (0)
Save
Tweet
6.12K Views

Join the DZone community and get the full member experience.

Join For Free
One of the core features of ASP.NET MVC that makes everything “just work” is the concept of routing.  By specifying ahead of time what a route looks like, we can create links that look like regular URLs with no parameters that behave like parameterized URLs on the server.

The magic for this all happens in Global.asax.cs (or .vb if you are using Visual Basic)

Open up the project you created last week and take a look at the Global.asax.cs file and you will find the following code:

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new {
controller = "Home",
action = "Index",
id = "" } // Parameter defaults
);

}

The first line,

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

is saying, “ignore anything that has an AXD extension.

The second line,

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new {
controller = "Home",
action = "Index",
id = "" } // Parameter defaults
);

Is saying, “create a route named default that interprets a URL in the form of Directory/Directory/Directory and have it call the Controller class using the first directory name, a method in that controller class using the second directory name and a parameter using the third directory name.  If nothing is passed that matches, then just call the ‘Index’ method of the ‘Home’ controller without any parameters.”

In the sample code, you’ll see that ID is an integer.  But, there is no reason why this parameter could not be a string.  There is also no reason why you couldn’t have multiple parameters if you needed or multiple routes if that makes sense.  You do, however, need at least one route.

You may want to consider NOT using integers for parameters in this scheme.  You may need a lookup table to achieve this but the advantage of not using integers is that your URLs look more “normal” and will be easier for the site visitor to remember.

Which would you rather remember?

http://somesite.com/Recipes/Display/ChickenSalad

or

http://somesite/Recipes/Display/23 ?

 

ASP.NET MVC ASP.NET

Published at DZone with permission of Dave Bush, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Utilize Python Machine Learning Models
  • Suspicious Sortings in Unity, ASP.NET Core, and More
  • What Are Cookies in Servlets?
  • Why to Implement GitOps into Your Kubernetes CI/CD Pipelines

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