How can I create a friendly URL in ASP.NET MVC?

前端 未结 3 1414
眼角桃花
眼角桃花 2020-11-28 03:23

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         


        
3条回答
  •  再見小時候
    2020-11-28 03:56

    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" });
    

提交回复
热议问题