If you want to serve extensionless URL from ASP.NET 2.0 like the following:
- www.store.com/books
- www.store.com/books/asp.net2.0
- www.forum.com/post/how-to-serve-extensionless-url
You cannot, unless you use some third party ISAPI module or use IIS 6.0 Wildcard mapping feature. Third party ISAPI module needs to be installed on the server directly. If you don’t have a dedicated server or a VPS, you cannot do this. IIS 6.0 Wildcard mapping makes each and every request go through ASP.NET 2.0 ISAPI handler including Urls with .gif, .css, .js, .html etc. So, it suffers from scalability problem. Some independent research shows there’s 30% drop in performance in IIS 6.0 when you use wildcard mapping. So, this is not a good solution either.
Here’s an approach which works without using ISAPI module or wildcard mapping in IIS 6.0. When you request extensionless URL, you get HTTP 404. This means IIS receives the request but it serves the page configured for HTTP 404. It does not send the request to ASP.NET ISAPI. So, if you can forward all HTTP 404 to ASP.NET, you can serve such extensionless URL. In order to forward HTTP 404 to ASP.NET ISAPI, all you need to do is configure IIS to redirect to some .aspx extension on HTTP 404.
Benefits of this approach:
- No thirdparty ISAPI module required
- No Wildcard mapping thus no performance sacrifice
Here’s how to configure 404 redirection in IIS 6.0:

On IIS 6.0 change 404 default page to /404.aspx and the type to “URL”. On IIS 7.0, change 404 default page to /404.aspx and the type to “ExecuteURL”. Also, change the default error response to “Custom error pages”.

When you will request an URL like “www.shop.com/products/books” it will redirect to “www.shop.com/404.aspx?404;http://www.shop.com/products/books”. From Global.asax BeginRequest event, capture this URL and see whether it’s an extensionless URL request. If it is, do your URL rewriting stuff for such extensionless URL.
1: protected void Application_BeginRequest(object sender, EventArgs e)
2: {
3: string url = HttpContext.Current.Request.Url.AbsolutePath;
4:
5: // HTTP 404 redirection for extensionless URL or some missing file
6: if (url.Contains("404.aspx"))
7: {
8: // On 404 redirection, query string contains the original URL in this format:
9: // 404;http://localhost:80/Http404Test/OmarALZabir
10:
11: string[] urlInfo404 = Request.Url.Query.ToString().Split(';');
12: if (urlInfo404.Length > 1)
13: {
14: string originalUrl = urlInfo404[1];
15:
16: string[] urlParts = originalUrl.Split('?');
17:
18: string queryString = string.Empty;
19: string requestedFile = string.Empty;
20:
21: if (urlParts.Length > 1)
22: {
23: requestedFile = urlParts[0];
24: queryString = urlParts[1];
25: }
26: else
27: {
28: requestedFile = urlParts[0];
29: }
30:
31: if( requestedFile.IndexOf('.') > 0 )
32: {
33: // There's some extension, so this is not an extensionless URL.
34: // Don't handle such URL because these are really missing files
35: }
36: else
37: {
38: // Extensionless URL. Use your URL rewriting logic to handle such URL
39: // I will just add .aspx extension to the extension less URL.
40: HttpContext.Current.RewritePath(requestedFile + ".aspx?" + queryString);
41: }
42: }
43: }
44: }
No related posts.















by re: Serve extensionless URL from ASP.NET without using ISAPI module or IIS 6 Wildcard mapping
01 May 2008 at 01:54
IIS will remove the If-Modified-Since header when redirecting to asp.net via the 404 page, other headers will remain intact.
I.e.
User sends If-Modified-Since: {date}, IIS strips it out, then redirects to 404 page. asp.net can then read any other header, like User Agent.
I am not sure why IIS would exhibit this behaviour.
by re: Serve extensionless URL from ASP.NET without using ISAPI module or IIS 6 Wildcard mapping
10 Jun 2008 at 09:54
Hai nice article Omer but i am getting an error like Cannot write virtual directory when using with IIS5.0 can u help on this
by re: Serve extensionless URL from ASP.NET without using ISAPI module or IIS 6 Wildcard mapping
26 Jun 2008 at 02:30
Very nice, was having a headache trying to come up with a solution for this. Thanks for the post.
One comment, is it really necessary to ToString() the Request.URL.Query? Seems it returns a string already?
-pete
by re: Serve extensionless URL from ASP.NET without using ISAPI module or IIS 6 Wildcard mapping
20 Aug 2008 at 03:35
Hi Omar, great solution. Is it possible the querystring wont be fired sometimes?
by re: Serve extensionless URL from ASP.NET without using ISAPI module or IIS 6 Wildcard mapping
02 Nov 2008 at 06:58
I absolutly love you ;o)!!! Good solution to a very annoying problem!!
by re: Serve extensionless URL from ASP.NET without using ISAPI module or IIS 6 Wildcard mapping
21 Nov 2008 at 12:33
I have a requirement to Inspect, the HttpMethod, From my client I am using a “POST”, but by the time it reaches the Module it has been changed to a “GET”. How can you ensure that the Post get passed properly?
Thanks,
by re: Serve extensionless URL from ASP.NET without using ISAPI module or IIS 6 Wildcard mapping
29 Mar 2009 at 12:27
OMG awesome, it took me exactly 5 min to solve a problem i had for a week. Url Rewritting is just a pain in the ass!!!
Thanks a lot Omar
by re: Serve extensionless URL from ASP.NET without using ISAPI module or IIS 6 Wildcard mapping
18 Jun 2009 at 12:59
This is a good approach. Sending 404s to aspnet seems much better than sending every request.
by re: Serve extensionless URL from ASP.NET without using ISAPI module or IIS 6 Wildcard mapping
16 Jul 2009 at 06:51
I am using this on local machine in IIS 5.1 it is working fine, but when i upload it on pre production with IIS6.0 t is not working.
please suggest
by Pankaj Bhasin
20 Jul 2010 at 19:03
Sir, I’ve used same procedure. But again this is NOT Working. Can u just suggest me. Should i use Mapping. Using Shared server.
by Abdul Muheet
04 Jun 2011 at 14:23
Sir,
I have implemented this solution in one of our site, It’s seems fine but for some user it showing 404.aspx page that we had set in IIS(while debugging i come to know that Begin_request event is not fire for some request), Could you please tell me the reason behind this Strange behaviour.
Regards,
Abdul Muheet
by Omar AL Zabir
04 Jun 2011 at 21:20
For what type of requests Begin_Request is not firing? WCF Service? HTML page?
by Abdul Muheet
05 Jun 2011 at 00:20
Thanks for your reply Sir, Actually I had implemented it for aspx 2.0 website, For some machines it working fine but some users are complaining that they are getting the 404.aspx page. For Testing this I wrote a code that append current requested URL in txt file on Application_BeginRequest event in global.asax file, So when anyone request any page that URL will be appended in txt file, but for some users this event will not fire at all and url not appeneded on file, Is this Event fire at the start of the Application or for every request of each n every users this will be execute?, Please help me. Jazakallah..
Regards,
Abdul Muheet
by Abdul Muheet
07 Jun 2011 at 12:24
Hi Sir,
I have figured it out it wasn’t Application_BeginRequest issue it was occured when someone type site URL without the www and i am replace http://www.sitename.com in my code. So now the issue has been resolved. Thanks
by Phil
15 Jan 2012 at 23:13
I could kiss you! Thank you! I *knew* this was possible, and have spent ages looking for it. Have asked on forums on how to do this, and got nowhere. (Favoured response there, if any, was MVC and URL ReWriter moduile – yuk.) The simplest answer is always the best.