在 ASP.NET MVC 中创建搜索引擎友好的 URL

发布于 2024-07-27 19:52:00 字数 977 浏览 7 评论 0原文

我想开发如下所示的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苦行僧 2024-08-03 19:52:00

在更深入的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!

面如桃花 2024-08-03 19:52:00

一个简单的选择是使用访问器将属性添加到模型对象,该访问器将适当的字段(在本例中为简短描述)规范化为合适的“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.

烂柯人 2024-08-03 19:52:00

这里的标准做法是为每个帖子存储一个“slug”,该“slug”将用作帖子的向外 URL。 例如,您的上述帖子的 slug 是:

best-product-in-the-world

一个不错的 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:

best-product-in-the-world

A decent CMS will do this for you automatically, and allow you to tweak the slug before saving.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文