Web窗体项目中的小写URL

Amg*_*tom 3 c# asp.net webforms

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

例如:

http://example.com/About

http://example.com/about

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

Ari*_*tos 8

global.asaxBeginRequest上,您可以简单地进行检查,重定向为:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

您还可以检查强制该规则的文件,例如仅检查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);
    }
}
Run Code Online (Sandbox Code Playgroud)

使用永久重定向

对于asp.net 4版本,您可以直接使用HttpResponse.RedirectPermanent For 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

以及将的代码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);
    }
}
Run Code Online (Sandbox Code Playgroud)

评论

我首先测试它并且正在工作,比制定规则更快,并且你可以更直接地控制它.

  • @Amgad 这个编辑后的答案看起来相当不错。虽然我认为你想要给出一个适当的 301 永久重定向响应,而不是这个 302。另外,像这样执行“Response.Redirect”会对性能产生负面影响,如[在这个答案中](http://stackoverflow.com)所述/questions/13727422/can-endresponse-increase-performance-of-asp-net-page/13727769#13727769)。如果您可能对每个页面请求都执行此操作,那么应该考虑这一点。 (2认同)