39

How do I generate friendly URLs within the ASP.NET MVC Framework? For example, we've got a URL that looks like this:

http://site/catalogue/BrowseByStyleLevel/1

The 1 is Id of the study level (Higher in this case) to browse, but I'l like to reformat the URL in the same way StackOverflow does it.

For example, these two URLs will take you to the same place:

https://stackoverflow.com/questions/119323/nested-for-loops-in-different-languages

https://stackoverflow.com/questions/119323/

EDIT: The friendly part of the url is referred to as a slug.

3
  • What kind of url do you want to see in the end, this one /catalogue/BrowseByStyleLevel/1/Higher? or /catalogue/BrowseByStyleLevel/Higher? Commented Oct 20, 2008 at 10:35
  • Ideally the second, but I need the Id to save looking up based on the text each time. Commented Oct 20, 2008 at 14:01
  • Please see stackoverflow.com/a/20662188/1298685 for an ASP.NET MVC 5 solution. Commented Dec 24, 2013 at 9:37

3 Answers 3

52

There are two steps to solve this problem. First, create a new route or change the default route to accept an additional parameter:

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

Now you can type whatever you want to at the end of your URI and the application will ignore it.

When you render the links, you need to add the "friendly" text:

<%= Html.ActionLink("Link text", "ActionName", "ControllerName",
                    new { id = 1234, ignoreThisBit="friendly-text-here" });
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, if I try this, the url generated from Html.ActionLink comes out like this: /Catalogue/BrowseBySubject/3?subject=chemistry instead of /Catalogue/BrowseBySubject/3/chemistry Any ideas - I've added the route below the 'Default' route, and changed the name to 'BrowseBySubject".
That means it's not finding the right route. Move the route above Default (which will hide default, if nothing else distinguishes them, like a constraint). Use a constraint to make this new route be found only when needed (e.g., on Catalogue/BrowseBySubject, or whatever your rule is).
@CraigStuntz ,your method seems easy compared to others. but will this be SEO friendly ?
3

This is how I have implemented the slug URL on my application. Note: The default Maproute should not be changed and also the routes are processed in the order in which they're added to the route list.

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

1 Comment

Thanks for that note, route matching order truly does matter, as in most frameworks.
1

you have a route on the global.asax

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

you can define your own route like :

controller is the cs class inside the the controllers folder.

you can define your id - with the name you choose.

the system will pass the value to your actionResult method.

you can read more about this step here : http://www.asp.net/learn/mvc/tutorial-05-cs.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.