我怎样才能创建ASP.NET MVC友好的网址? [英] How can I create a friendly URL in ASP.NET MVC?

查看:167
本文介绍了我怎样才能创建ASP.NET MVC友好的网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何生成ASP.NET MVC框架内的友好的网址?例如,我们已经得到了类似如下的URL:

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

的1是在研究水平(高等在这种情况下),浏览的ID,但I'l喜欢重新格式化的URL以同样的方式计算器做它

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.

例如,这两个网址,会带你到相同的地方:

For example, these two URLs will take you to the same place:

<一个href=\"http://stackoverflow.com/questions/119323/nested-for-loops-in-different-languages\">http://stackoverflow.com/questions/119323/nested-for-loops-in-different-languages

http://stackoverflow.com/questions/119323/

编辑:的URL友好的部分被称为的蛞蝓

The friendly part of the url is referred to as a slug.

推荐答案

有两个步骤来解决这个问题。首先,创建一个新的路由或更改默认的路由,接受额外的参数:

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 )

现在你可以在你的URI的末尾键入任何你想,应用程序将忽略它。

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

这篇关于我怎样才能创建ASP.NET MVC友好的网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆