3

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

e.g:

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.

Majid
  • 13,853
  • 15
  • 77
  • 113
Amged Rustom
  • 1,739
  • 3
  • 19
  • 25

1 Answers1

8

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.

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • If it break parameters, that could be a big issue. Not only for his code, but I think *.axd and bundling will also be broken. – MikeSmithDev Dec 28 '13 at 13:31
  • @MikeSmithDev I have update it. Tested with parametres and working. – Aristos Dec 28 '13 at 13:32
  • Thanks. Works perfectly with file and folder names as well as with parameters. For bundling I only checked using FireBug to see if any file was not found by the browser. Aristos and @MikeSmithDev is there anything else I should check before pushing the code to production? – Amged Rustom Dec 28 '13 at 15:38
  • 1
    @Amgad This edited answer looks pretty good. Though I would think you'd want to give a proper 301 permanent redirect response instead this 302. Plus, doing `Response.Redirect` like that can negatively impact performance, as noted [in this answer](http://stackoverflow.com/questions/13727422/can-endresponse-increase-performance-of-asp-net-page/13727769#13727769). If you are potentially doing this for every page request, it should be something to consider. – MikeSmithDev Dec 28 '13 at 18:38
  • @Aristos if I would put that much effort into it, I'd just answer myself ;) I'm just thinking of things he should consider but I just don't have enough time to craft up a complete answer. – MikeSmithDev Dec 28 '13 at 18:42
  • 1
    Thanks, the [ protected void Application_BeginRequest ] works for me!!!! – Lester Dec 14 '16 at 21:20