Making the URL of a request lowercase in global.asax or an HTTP Module in ASP.NET 2.0

Hello,
I have an ASP.NET 2.0 application that runs in IIS 6.0 and I wanted to know if there is a way to convert any incoming page request to lowercase (e.g. from http://www.site.com/Folder/SUBFOLDER/PageInPascal.aspx tp http://www.site.com/folder/subfolder/pageinpascal.aspx).  I can't use the routing features in IIS 7.0 and rewriting features in ASP.NET 3.5.

Is there a way to take the current request and make it lower case in either the global.asax file or an HTTP Module and how would I do that?
endrecAsked:
Who is Participating?
 
masterpassConnect With a Mentor Commented:
You can use the ToLower() to redirect. I have attached the code how to go about it and it will NOT create a infinite loop. Also this is a code snippet we use in our projects
// Check url 
if (Regex.IsMatch(HttpContext.Current.Request.Url.ToString(), @"[A-Z]"))
{

    // Lowercase url  
    String lower = HttpContext.Current.Request.Url.ToString().ToLower();

    // Redefine response  
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.Status = "301 Moved Permanently";
    HttpContext.Current.Response.AddHeader("Location", lower);
    HttpContext.Current.Response.End();

}

Open in new window

0
 
masterpassCommented:
Try this code, its very simple

It is in the Application_BeginRequest of global.asax
protected void Application_BeginRequest(object sender, EventArgs e)
{
string requestedUrl = Request.Url.AbsoluteUri.ToLower();
//do the rest here 
}

Open in new window

0
 
Carl TawnSystems and Integration DeveloperCommented:
Converting to lowercase is simple enough. What is the context? Where do you want it to appear in lower case, or why specifically do you need it to be lower case?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
endrecAuthor Commented:
The files on the site are named using different casing and frankly some users find it strange looking.  It's purely for that reason.  The context would be the current request.

So if the user, or a link points to, ~/FoLder/PAGE.aspx when the user clicks that link or is redirected that page via an Respons.Redirect how could I dynamically convert the entire URL that appears in their browser's address bar to lowercase (including all querystrings in the URL).
0
 
Carl TawnSystems and Integration DeveloperCommented:
You might have to resort to using javascript to accomplish what you want, otherwise you're going to have to do a Redirect on each request, which is going to double the hits on your server.
0
 
endrecAuthor Commented:
The redirect would actually be fine in this case.
0
 
masterpassCommented:
In that case tolower() is the simplest one that you can do ..  
0
 
endrecAuthor Commented:
Hello Masterpass,
So would using the code you outlines for Application_BeginRequest convert the user's current request (including all the querystrings) URL to lower case or is there something extra that needs to be added for the querystrings to also be included?

Thank you for your help
0
 
Carl TawnSystems and Integration DeveloperCommented:
Masterpass's suggestion of simply putting ToLower() in the BeginRequest won't work because doing a Response.Redirect from that method call will simply create an infinite loop.
0
 
endrecAuthor Commented:
Hi Masterpass,
I turned this into an HttpModule, but I wondered if you ever run into exceptions on Response.End with the thread being aborted.  You have masterfully answered my query, however I just wanted to inquire if you see any problems if I use Response.End.

http://www.west-wind.com/WebLog/posts/368975.aspx
http://www.hanselman.com/blog/AnIPAddressBlockingHttpModuleForASPNETIn9Minutes.aspx (see comments)
0
All Courses

From novice to tech pro — start learning today.