简体   繁体   中英

Lower case URLs in Web Forms project

I'm developing a web forms application in Visual Studio 2013 and would like to make the URLs lower case.

eg:

http://example.com/About

to

http://example.com/about

All the solutions I found were via IIS rewriting rules but I would like to solve it in the project itself.

On global.asax on the BeginRequest you simple make your check, and the redirect as:

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);
    }
}

You can also check which file to force that rule, for example check only the aspx files:

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);
    }
}

With Permanent Redirect

For the asp.net 4 version you can direct use the HttpResponse.RedirectPermanent For the asp.net 3.5 versions and before I make a similar redirect with asp.net but with 301 Moved Permanently response:

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();
    }
}

and the code on the protected void Application_BeginRequest(Object sender, EventArgs e) will be:

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);
    }
}

Comments

I first test it and is working, is faster than make rules, and you have more direct control on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM