如何在 ASP.NET MVC 框架中生成友好的 URL?例如,我们有一个如下所示的 URL:
http://site/catalogue/BrowseByStyleLevel/1
1 是要浏览的学习级别(在本例中为较高级别)的 Id,但我想以与 StackOverflow 相同的方式重新格式化 URL。
例如,这两个 URL 将带您到同一个地方:
https://stackoverflow.com/questions/119323/nested-for-loops-in- different-languages https://stackoverflow.com/questions/119323/nested-for-loops-in-different-languages
https://stackoverflow.com/questions/119323/ https://stackoverflow.com/questions/119323/
EDIT:url 的友好部分称为slug.
解决这个问题有两个步骤。首先,创建一个新路由或更改默认路由以接受附加参数:
routes.MapRoute( "Default", // Route name
"{controller}/{action}/{id}/{ignoreThisBit}",
new { controller = "Home",
action = "Index",
id = "",
ignoreThisBit = ""} // Parameter defaults )
现在,您可以在 URI 末尾键入您想要的任何内容,应用程序将忽略它。
当您呈现链接时,您需要添加“友好”文本:
<%= Html.ActionLink("Link text", "ActionName", "ControllerName",
new { id = 1234, ignoreThisBit="friendly-text-here" });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)