Kie*_*ron 39 c# asp.net-mvc friendly-url asp.net-mvc-routing slug
如何在ASP.NET MVC框架中生成友好的URL?例如,我们有一个如下所示的URL:
http://site/catalogue/BrowseByStyleLevel/1
1是研究级别的Id(在这种情况下是更高的)来浏览,但我想以与StackOverflow相同的方式重新格式化URL.
例如,这两个网址会将您带到同一个地方:
编辑:网址的友好部分被称为slug.
Cra*_*ntz 51
有两个步骤可以解决这个问题.首先,创建新路由或更改默认路由以接受其他参数:
routes.MapRoute( "Default", // Route name
"{controller}/{action}/{id}/{ignoreThisBit}",
new { controller = "Home",
action = "Index",
id = "",
ignoreThisBit = ""} // Parameter defaults )
Run Code Online (Sandbox Code Playgroud)
现在,您可以在URI的末尾键入任何内容,应用程序将忽略它.
渲染链接时,需要添加"友好"文本:
<%= Html.ActionLink("Link text", "ActionName", "ControllerName",
new { id = 1234, ignoreThisBit="friendly-text-here" });
Run Code Online (Sandbox Code Playgroud)