【问题标题】:MVC - Make all URLs with a trailing slash 301 to URL without a slash?MVC - 使所有带有斜杠的 URL 301 变成没有斜杠的 URL?
【发布时间】:2016-11-29 00:50:29
【问题描述】:

有没有办法将所有以斜杠结尾的网址重定向到不带斜杠的网址?

【问题讨论】:

    标签: asp.net routes


    【解决方案1】:

    Global.asaxApplication_BeginRequest 方法中,添加以下内容:

    if (HttpContext.Current.Request.Url.AbsolutePath != "/" && HttpContext.Current.Request.Url.AbsolutePath.EndsWith("/")) {
        string redirect = HttpContext.Current.Request.Url.AbsolutePath;
        redirect = redirect.Remove(redirect.Length - 1);
        Response.Clear();
        Response.Status = "301 Moved Permanently";
        Response.AddHeader("Location", redirect);
        Response.End();
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-16
    • 1970-01-01
    • 2011-09-13
    相关资源
    最近更新 更多