在 ASP.NET MVC 中创建搜索引擎友好的 URL
我想开发如下所示的 URL:
http://mysite.com/products/1/best-product-in-the-world
我需要获取正确记录的是以下路线:
http://mysite.com/products/1
当我将产品描述部分添加到 URL 时(“世界上最好的产品”)我遇到 URL 编码问题。 我在 ActionLink(...) 中构造 URL 的这一部分时尝试使用 Server.UrlEncode:
<%= Html.ActionLink(item.Subject, "../Post/Detail",
new { id = item.ID,
descriptiveUrl = Server.UrlEncode(Product.ShortDescription) },
new { rel = "canonical",
title = Product.ShortDescription,
@class = "product-hyperlink" })%>
但这会呈现特殊字符和空格的常规编码元素,非常类似于以下内容:
http://localhost:2392/Products/Detail/1/best+product+in+the+world253f
...这会创建一个 400,错误请求异常。 不确定我是否公正地回答了这个问题,但如果需要的话可以提供进一步的说明。
更新:这篇文章的网址如下,我正在尝试做一些非常相似的事情!
http://stackoverflow.com/questions/1148955/creating-search-engine-friendly-urls-in-asp-net-mvc
I would like to develop URL's which look like the following:
http://mysite.com/products/1/best-product-in-the-world
Where all i need to get to the proper record is the following route:
http://mysite.com/products/1
When I add the product description piece to the URL ("best-product-in-the-world") I get URL encoding issues. I've tried to use Server.UrlEncode when constructing this portion of my URL in an ActionLink(...):
<%= Html.ActionLink(item.Subject, "../Post/Detail",
new { id = item.ID,
descriptiveUrl = Server.UrlEncode(Product.ShortDescription) },
new { rel = "canonical",
title = Product.ShortDescription,
@class = "product-hyperlink" })%>
But this renders regularly encoded elements for special characters and spaces, much like the following:
http://localhost:2392/Products/Detail/1/best+product+in+the+world253f
...which creates a 400, bad request exception. Not sure I've done the question justice, but can provide further clarification if need be.
Update: this post's URL is as follows, and i'm trying to do something very similar!
http://stackoverflow.com/questions/1148955/creating-search-engine-friendly-urls-in-asp-net-mvc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在更深入的Google搜索中,我发现了以下用于生成slugs的链接:
http://www.intrepidstudios.com/blog/2009/2/10/function-to-generate-a-url-friend-string.aspx
谢谢@ Rob 和@Coding the Wheel 给了我找到这个答案真正需要的术语!
In a deeper Google search, I found the following link for generating slugs:
http://www.intrepidstudios.com/blog/2009/2/10/function-to-generate-a-url-friendly-string.aspx
Thanks @Rob and @Coding the Wheel for giving me the terminology I really needed to find this answer!
一个简单的选择是使用访问器将属性添加到模型对象,该访问器将适当的字段(在本例中为简短描述)规范化为合适的“slug”; 也就是说,标识符后面的垃圾文本位。 然后,您可以在构建 URI 时使用它。
规范化过程可能非常简单,只需删除所有非字母数字字符并用连字符替换空格即可。
A simple option would be to add a property to your model object with an accessor that normalises the appropriate field (short description in this case) down to a suitable "slug"; that is, the bit of junk text after the identifier. You then use this when constructing the URI.
The normalisation process might be as simple as removing any non-alphanumeric characters and replacing spaces with hyphens.
这里的标准做法是为每个帖子存储一个“slug”,该“slug”将用作帖子的向外 URL。 例如,您的上述帖子的 slug 是:
一个不错的 CMS 会自动为您完成此操作,并允许您在保存之前调整 slug。
The standard practice here is to store a 'slug' with each post that will function as the post's outward-facing URL. For example, your slug for the above post would be:
A decent CMS will do this for you automatically, and allow you to tweak the slug before saving.