简体   繁体   English

ASP.NET MVC路由/ SEO友好URL

[英]ASP.NET MVC Routing / SEO Friendly URL

I'm trying to do something like stackoverflow 我正在尝试做类似stackoverflow的事情

Take a link from stackoverflow for example: 以stackoverflow中的链接为例:

Hidden Features of C#? C#的隐藏功能?

if you remove the last part ( Hidden Features of C#? ) it still returns the same result. 如果删除最后一部分( C#的隐藏功能? ),它仍然返回相同的结果。

For my routing in Global.asax I tried doing something like "{action}/{id}/{title}" 对于在Global.asax中的路由,我尝试执行类似“ {action} / {id} / {title}”的操作

On my page, this is my link: 在我的页面上,这是我的链接:

<%= Html.ActionLink(video.Title, "Details", "Videos", new {id = video.ID, title = video.Title.Replace(" ", "-")}, null) %>

This does what I want it to do for the most part except that after the id it throws in " ?title=blah-blah-blah " 这样做基本上是我想要的,除了id后面会抛出“ ?title = blah-blah-blah

I want it to say " id/blah-blah-blah " 我想说“ id / blah-blah-blah

What's my problem? 我怎么了 (Besides being a noob) (除了是菜鸟)

I wrote a blog post on this a while back and thought it might be helpful: 不久前,我写了一篇博客文章,认为这可能会有所帮助:

http://web.archive.org/web/20170416234816/http://mynameiscoffey.com/2010/12/19/seo-friendly-urls-in-asp-net-mvc/ http://web.archive.org/web/20170416234816/http://mynameiscoffey.com/2010/12/19/seo-friendly-urls-in-asp-net-mvc/

Basically you need to check in your action for the presence of the correct SEO-Friendly title when your action is executing, and if it doesn't find it, issue a redirect back to the browser to the correct SEO-Friendly URL. 基本上,您需要在执行操作时检查操作中是否存在正确的SEO友好标题,如果找不到,请向浏览器发出重定向到正确的SEO友好URL。

That route looks like it should work with that call to ActionLink, so this is a bit of a guess. 该路由看起来应该可以与对ActionLink的调用一起使用,因此这有点猜测。 Are you registering your {action}/{id}/{title} route after the default route? 您要在默认路由之后注册{action}/{id}/{title}路由吗? If so, the default route will match first, and just put the title value in the querystring since it doesn't appear in the path. 如果是这样,则默认路由将首先匹配,并将标题值放入查询字符串中,因为它不会出现在路径中。 If you register your custom route before the {controller}/{action}/{id} default, it should work. 如果您在{controller}/{action}/{id}默认值之前注册了自定义路由,则该路由应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM