在ASP.NET MVC中创建搜索引擎友好的URL [英] Creating search engine friendly URL's in ASP.NET MVC

查看:74
本文介绍了在ASP.NET MVC中创建搜索引擎友好的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发如下URL:

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

当我将产品说明片添加到URL(世界上最好的产品")时,我遇到了URL编码问题.在ActionLink(...)中构造URL的这一部分时,我曾尝试使用Server.UrlEncode:

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

...将创建一个400错误的请求异常.不确定我是否已经解决了正义问题,但是如果需要的话,可以提供进一步的澄清.

...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

推荐答案

一个简单的选择是使用访问器向模型对象添加属性,该访问器将适当的字段(在这种情况下为简短描述)归一化为合适的"sl";也就是标识符后的一小段垃圾文字.然后在构造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.

这篇关于在ASP.NET MVC中创建搜索引擎友好的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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