【问题标题】:Lower case URLs in Web Forms projectWeb 窗体项目中的小写 URL
【发布时间】:2014-01-15 21:13:13
【问题描述】:

我正在 Visual Studio 2013 中开发 Web 表单应用程序,并希望将 URL 设为小写。

例如:

http://example.com/About

http://example.com/about

我找到的所有解决方案都是通过 IIS 重写规则,但我想在项目本身中解决它。

【问题讨论】:

    标签: c# asp.net webforms


    【解决方案1】:

    在 BeginRequest 上的 global.asax 上,您只需进行检查,然后重定向为:

    protected void Application_BeginRequest(Object sender, EventArgs e)     
    {
        // place the lower case on string, to avoid make it again later.
        string cTheLowerUrl = HttpContext.Current.Request.Path.ToLowerInvariant();
        if (cTheLowerUrl != HttpContext.Current.Request.Path)
        {
            HttpContext.Current.Response.Redirect(cTheLowerUrl + HttpContext.Current.Request.Url.Query);
        }
    }
    

    您还可以检查要强制执行该规则的文件,例如仅检查 aspx 文件:

    string sExtOfFile = System.IO.Path.GetExtension(HttpContext.Current.Request.Path);
    if (sExtOfFile.Equals(".aspx", StringComparison.InvariantCultureIgnoreCase))
    {
        string cTheLowerUrl = HttpContext.Current.Request.Path.ToLowerInvariant();
        if (cTheLowerUrl != HttpContext.Current.Request.Path)
        {
            HttpContext.Current.Response.Redirect(cTheLowerUrl + HttpContext.Current.Request.Url.Query);
        }
    }
    

    带有永久重定向

    asp.net 4 版本可以直接使用HttpResponse.RedirectPermanent 对于 asp.net 3.5 版本以及在我使用 asp.net 进行类似重定向但带有 301 Moved Permanently 响应之前:

    public static void RedirectPermanent(string url, bool endResponse = true)
    {
        HttpResponse responce = HttpContext.Current.Response;
    
        #if DEBUG
        if (url == null)
        {
            throw new ArgumentNullException("url");
        }
        if (responce == null)
        {
            throw new ArgumentNullException("url");
        }
        if (url.IndexOf('\n') >= 0)
        {
            throw new ArgumentException("Cannot_redirect_to_newline");
        }
    
        Page handler = HttpContext.Current.Handler as Page;
    
        if ((handler != null) && handler.IsCallback)
        {
            throw new ApplicationException("Redirect_not_allowed_in_callback");
        }
        #endif
    
        url = responce.ApplyAppPathModifier(url);
    
        responce.Clear();
        responce.TrySkipIisCustomErrors = true;
        responce.StatusCode = 301;
        responce.Status = "301 Moved Permanently";
        responce.RedirectLocation = url;
    
        // a direct header diferent way 
        // responce.AddHeader("Location", url);     
    
        responce.Write("<html><head><title>Object moved</title></head><body>\r\n");
        responce.Write("<h2>Object moved to <a href=\"" + url + "\">here</a>.</h2>\r\n");
        responce.Write("</body></html>\r\n");
    
        if (endResponse)
        {
            responce.End();
        }
    }
    

    protected void Application_BeginRequest(Object sender, EventArgs e) 上的代码将是:

    string sExtOfFile = System.IO.Path.GetExtension(HttpContext.Current.Request.Path);
    if (sExtOfFile.Equals(".aspx", StringComparison.InvariantCultureIgnoreCase))
    {
        string cTheLowerUrl = HttpContext.Current.Request.Path.ToLowerInvariant();
        if (cTheLowerUrl != HttpContext.Current.Request.Path)
        {
                // for asp.net 4 and above
                HttpContext.Current.Response.RedirectPermanent(cTheLowerUrl + HttpContext.Current.Request.Url.Query);
                // or using the above function.
                // RedirectPermanent(cTheLowerUrl + HttpContext.Current.Request.Url.Query);
        }
    }
    

    评论

    我第一次测试它并且正在运行,比制定规则更快,并且你可以更直接地控制它。

    【讨论】:

    • 如果它破坏了参数,那可能是个大问题。不仅是因为他的代码,而且我认为 *.axd 和捆绑也会被破坏。
    • @MikeSmithDev 我已经更新了。用参数和工作测试。
    • 谢谢。与文件和文件夹名称以及参数完美配合。对于捆绑,我只使用 FireBug 检查浏览器是否找不到任何文件。 Aristos 和@MikeSmithDev 在将代码推送到生产环境之前我还应该检查什么?
    • @Amgad 这个编辑后的答案看起来不错。虽然我认为您希望给出适当的 301 永久重定向响应而不是这个 302。另外,像 in this answer 所述,这样做可能会对性能产生负面影响。如果您可能对每个页面请求都这样做,那么应该考虑一下。
    • 谢谢,[ protected void Application_BeginRequest ] 对我有用!!!!
    猜你喜欢
    • 2012-08-03
    • 2014-10-15
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多