C# 如何在 ASP.NET MVC 中创建友好的 URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/217960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I create a friendly URL in ASP.NET MVC?
提问by Kieron
How do I generate friendly URLs within the ASP.NET MVC Framework? For example, we've got a URL that looks like this:
如何在 ASP.NET MVC 框架中生成友好的 URL?例如,我们有一个如下所示的 URL:
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.
1 是要浏览的研究级别的 ID(在本例中为较高),但我想以与 StackOverflow 相同的方式重新格式化 URL。
For example, these two URLs will take you to the same place:
例如,这两个 URL 会带你到同一个地方:
https://stackoverflow.com/questions/119323/nested-for-loops-in-different-languages
https://stackoverflow.com/questions/119323/nested-for-loops-in-different-languages
EDIT:The friendly part of the url is referred to as a slug.
编辑:url 的友好部分被称为slug。
采纳答案by Craig Stuntz
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.
现在您可以在 URI 的末尾键入任何您想要的内容,应用程序将忽略它。
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" });
回答by Moran Helman
you have a route on the global.asax
您在 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.
控制器是控制器文件夹中的 cs 类。
you can define your id - with the name you choose.
您可以定义您的 ID - 使用您选择的名称。
the system will pass the value to your actionResult method.
系统会将值传递给您的 actionResult 方法。
you can read more about this step here : http://www.asp.net/learn/mvc/tutorial-05-cs.aspx
您可以在此处阅读有关此步骤的更多信息:http: //www.asp.net/learn/mvc/tutorial-05-cs.aspx
回答by Hamid Tavakoli
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.
这就是我在我的应用程序上实现 slug URL 的方式。 注意:默认的 Maproute 不应更改,并且路由也会按照它们添加到路由列表的顺序进行处理。
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="" });