建立规范的URL包括一个ID和标题蛞蝓 [英] Creating Canonical URLs including an id and title slug

查看:145
本文介绍了建立规范的URL包括一个ID和标题蛞蝓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要复制什么计算器确实与它的URL。

I want to replicate what StackOverflow does with its URLs.

例如:

的隐藏的功能的C# - ?(http://stackoverflow.com/questions/ 9033 /隐藏功能 - 的-C)

Hidden Features of C#? - (http://stackoverflow.com/questions/9033/hidden-features-of-c)

的隐藏的功能的C# - ?(http://stackoverflow.com/questions/9033/)

Hidden Features of C#? - (http://stackoverflow.com/questions/9033/)

将带你到相同的页面,但是当他们返回到浏览器总是返回第一个。

Will Take you to the same page but when they return to the browser the first one is always returned.

如何实现的变化,从而返回较大的网址是什么?

How do you implement the change so the larger URL is returned?

推荐答案

这是我以前这样处理是有两条路线的方式,按照这个顺序登记

The way that I've handled this before is to have two routes, registered in this order

routes.MapRoute(
    null,
    "questions/{id}/{title}",
    new { controller = "Questions", action = "Index" },
    new { id = @"\d+", title = @"[\w\-]*" });

routes.MapRoute(
    null,
    "questions/{id}",
    new { controller = "Questions", action = "Index" },
    new { id = @"\d+" });

现在在控制器动作,

public class QuestionsController 
{
    private readonly IQuestionRepository _questionRepo;

    public QuestionsController(IQuestionRepository questionRepo)
    {
        _questionRepo = questionRepo;
    }

    public ActionResult Index(int id, string title)
    {
        var question = _questionRepo.Get(id);

        if (string.IsNullOrWhiteSpace(title) || title != question.Title.ToSlug())
        {
            return RedirectToAction("Index", new { id, title = question.Title.ToSlug() }).AsMovedPermanently();
        }

        return View(question);
    }
}

我们会永久重定向到包含标题蛞蝓(带连字符作为分隔符小写标题),如果我们只拥有ID的URL。我们还确保传递的标题是正确的通过检查它反对该议题标题的猛击版本,从而为同时包含ID和正确的标题塞问题的规范网址。

We'll permanently redirect to the URL that contains the title slug (lowercase title with hyphens as separators) if we only have the id. We also make sure that the title passed is the correct one by checking it against the slugged version of the question title, thereby creating a canonical URL for the question that contains both the id and the correct title slug.

用一对夫妇的帮手

public static class PermanentRedirectionExtensions
{
    public static PermanentRedirectToRouteResult AsMovedPermanently
        (this RedirectToRouteResult redirection)
    {
        return new PermanentRedirectToRouteResult(redirection);
    }
}

public class PermanentRedirectToRouteResult : ActionResult
{
    public RedirectToRouteResult Redirection { get; private set; }
    public PermanentRedirectToRouteResult(RedirectToRouteResult redirection)
    {
        this.Redirection = redirection;
    }
    public override void ExecuteResult(ControllerContext context)
    {
        // After setting up a normal redirection, switch it to a 301
        Redirection.ExecuteResult(context);
        context.HttpContext.Response.StatusCode = 301;
        context.HttpContext.Response.Status = "301 Moved Permanently";
    }
}

public static class StringExtensions
{
    private static readonly Encoding Encoding = Encoding.GetEncoding("Cyrillic");

    public static string RemoveAccent(this string value)
    {
        byte[] bytes = Encoding.GetBytes(value);
        return Encoding.ASCII.GetString(bytes);
    }

    public static string ToSlug(this string value)
    {
        if (string.IsNullOrWhiteSpace(value))
        {
            return string.Empty;
        }

        var str = value.RemoveAccent().ToLowerInvariant();

        str = Regex.Replace(str, @"[^a-z0-9\s-]", "");

        str = Regex.Replace(str, @"\s+", " ").Trim();

        str = str.Substring(0, str.Length <= 200 ? str.Length : 200).Trim();

        str = Regex.Replace(str, @"\s", "-");

        str = Regex.Replace(str, @"-+", "-");

        return str;
    }
}

这篇关于建立规范的URL包括一个ID和标题蛞蝓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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