ASP.NET MVC Routing / SEO Friendly URL

淺唱寂寞╮ 提交于 2019-12-18 12:29:28

问题


I'm trying to do something like stackoverflow

Take a link from stackoverflow for example:

Hidden Features of C#?

if you remove the last part (Hidden Features of C#?) it still returns the same result.

For my routing in Global.asax I tried doing something like "{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"

I want it to say "id/blah-blah-blah"

What's my problem? (Besides being a noob)


回答1:


That route looks like it should work with that call to ActionLink, so this is a bit of a guess. Are you registering your {action}/{id}/{title} route after the default route? 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.




回答2:


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/

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.



来源:https://stackoverflow.com/questions/3620092/asp-net-mvc-routing-seo-friendly-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!