添加ID和标题在ASP.NET MVC URL蛞蝓 [英] Adding ID and title to URL slugs in ASP.NET MVC

查看:142
本文介绍了添加ID和标题在ASP.NET MVC URL蛞蝓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在ASP.NET MVC重定向请求到正确的规范版本,如果URL部分缺失?

How do you redirect a request in ASP.NET MVC to its correct canonical version if part of the URL is missing?

使用堆栈溢出,例如,在网站上增加的问题标题到其路由的端,但是使用的问题ID与路线实际找到的问题。如果标题被省略了,你会被重定向到正确的URL。

Using Stack Overflow as an example, the site adds the question title to the end of its routes, but uses the question ID in the route to actually find the question. If the title gets omitted you will be redirected to the correct URL.

例如,访问网址:

stackoverflow.com/questions/9033 

会重定向到

stackoverflow.com/questions/9033/hidden-features-of-c

这是如何工作的?

How does this work?

推荐答案

首先创建一个路由:

routes.MapRoute( 
    "ViewProduct", 
    "Products/{id}/{productName}", 
    new { controller = "Product", action = "Details", id = "", productName = "" } 
);

然后创建操作方法如下所示:

Then create the Action method like so:

public ActionResult Details(int? id, string productName) 
{ 
    Product product = ProductRepository.Fetch(id); 

    string realTitle = UrlEncoder.ToFriendlyUrl(product.Title); 
    string urlTitle = (productName ?? "").Trim().ToLower(); 

    if (realTitle != urlTitle)
    { 
        string url = "/Products/" + product.Id + "/" + realTitle; 
        return new PermanentRedirectResult(url);
    } 

    return View(product); 
}

你基本上比较与存储在数据库中,如果他们不匹配,则执行301永久重定向一个URL中的实体名称。确保它是一个永久性重定向(301状态code),而不是一个临时的重定向(302)。这样,搜索引擎会将其作为URL的永久变化,并相应地更新其索引,如果你的实体的标题更改了搜索引擎索引之后(例如有人更改产品的名称),这可能发生。

You're basically comparing the entity title in the URL with the one stored in the database, if they don't match then perform a 301 permanent redirect. Make sure it's a 'permanent' redirect (301 status code) instead of a temp redirect (302). This way search engines will treat it as a permanent change of the URL and will update their indexes accordingly, this might happen if the title of your entity changes after a search engine has indexed it (e.g. someone changes the name of the product).

另一件事要注意,如果你的标题允许任何自由文本,你需要去掉那些对URL无效的任何字符,并使其对人类和搜索引擎一样,因此UrlEn $ C $更具可读性在上面的code cr.ToFriendlyUrl法,实现如下:

Another thing to be aware of, if your title allows any free text, you need to strip out any characters that are invalid for a URL, and make it more readable for humans and search engines alike, hence the UrlEncoder.ToFriendlyUrl method in the code above, the implementation is below:

public static class UrlEncoder 
{ 
    public static string ToFriendlyUrl (this UrlHelper helper, 
        string urlToEncode) 
    { 
        urlToEncode = (urlToEncode ?? "").Trim().ToLower(); 

        StringBuilder url = new StringBuilder(); 

        foreach (char ch in urlToEncode) 
        { 
            switch (ch) 
            { 
                case ' ': 
                    url.Append('-'); 
                    break; 
                case '&': 
                    url.Append("and"); 
                    break; 
                case '\'': 
                    break; 
                default: 
                    if ((ch >= '0' && ch <= '9') || 
                        (ch >= 'a' && ch <= 'z')) 
                    { 
                        url.Append(ch); 
                    } 
                    else 
                    { 
                        url.Append('-'); 
                    } 
                    break; 
            } 
        } 

        return url.ToString(); 
    } 
}

所以,当你写出来的网址进入查看,一定要带code用这种方法例如标题

So when you write out the URLs into the View, be sure to encode the titles with this method e.g.

<a href="/Products/@Model.Id/@Url.ToFriendlyUrl(Model.Title)">@Model.Title</a>

我已经写了一篇博客文章这个位置<一个href=\"http://www.dominicpettifer.co.uk/Blog/34/asp-net-mvc-and-clean-seo-friendly-urls\">http://www.dominicpettifer.co.uk/Blog/34/asp-net-mvc-and-clean-seo-friendly-urls

这篇关于添加ID和标题在ASP.NET MVC URL蛞蝓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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