How do I generate friendly URLs within the ASP.NET MVC Framework? For example, we've got a URL that looks like this:
如何在ASP中生成友好的url。净MVC框架?例如,我们有一个这样的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 * does it.
1是要浏览的研究级别的Id(在本例中更高),但是我希望以*那样的方式重新格式化URL。
For example, these two URLs will take you to the same place:
例如,这两个url将把您带到相同的位置:
https://*.com/questions/119323/nested-for-loops-in-different-languages
https://*.com/questions/119323/nested-for-loops-in-different-languages
https://*.com/questions/119323/
https://*.com/questions/119323/
EDIT: The friendly part of the url is referred to as a slug.
编辑:url的友好部分被称为蛞蝓。
3 个解决方案
#1
47
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" });
#2
1
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.
controller是controller文件夹中的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
#3
1
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="" });
#1
47
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" });
#2
1
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.
controller是controller文件夹中的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
#3
1
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="" });