Check out JobNimbus - CRM for Contractors and Service Professionals.
Global.asax Events in IIS 6 and IIS 7 for Static Resources
A quick and easy way of doing URL rewriting or responding to requests for static resources is to use the Application_BeginRequest event. This event will fire each time a page is requested. In there, you could do something like the following to serve a different file when a specific URL is requested to your site:
// get the current URL string sUrl = Request.Url.PathAndQuery; if (sUrl == "/my-pretty-url/some-page") { // rewrite the URL to a different path // (NOTE: user's browser's address will stay the same) HttpContext.Current.RewritePath("/SomeOtherUrl.aspx"); }
You could also block access to a static resource like an image from being served based on some criteria. In this code sample, I’ve blocked access to an image if the caller is a specific website I don’t want to serve my images to:
// get the current URL string sUrl = Request.Url.PathAndQuery; // check the IP address of the caller if (Request.UserHostAddress == "122.134.54.234" && sUrl == "/images/my-image-name.png") { // serve up 404 error because we don't want this client to get access // to this image resource Response.Status = "404 Not Found"; Response.StatusCode = 404; Response.End(); }
These code samples will run great in your Visual Studio 2008 or 2010 Cassini built-in web server, but if you deploy them to a standard IIS 6 or IIS 7 server, you will be scratching your head because your Global.asax events will not fire when static resources are requested.
Problem in IIS 6
For IIS 6, the reason for this is that the IIS pipeline does not support native .NET IHttpModules. Pre-IIS 7, .NET was an ISAPI plug-in for IIS instead of native to the server. Static resources get served by IIS using the built-in IIS services. Therefore, static resources do not go through the ASP.NET pipeline and your events are not fired.
IIS 6 Solution
There are several solutions to get IIS 6 to run your Global.asax file events whenever a static resource is served. The easiest to implement is to add a wild card mapping to your server to run all requests through the .NET run-time. To do this, first right-click on your IIS web site instance that you want to change and choose properties.

Click on the Home Directory tab and then click "Configuration" next to your web application instance.

On the Wildcard application maps section, click "Insert" (NOTE: don’t click "Add")

Enter the path to your ASP.NET ISAPI dll. For instance, if you are using .NET 2.0, then your path would like this:
C:\windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll
If you are using .NET 4.0, then it will have a similar path.
Make sure to NOT check the box "Verify that file exists" and then click OK.

There are other solutions as well but this one is simplest. On high performance, high traffic sites, you may want to consider alternatives because this solution could affect performance since all static resources will be served by ASP.NET instead of the native IIS server (which could be slower in some cases). Other solutions and more information this is in this great article here:
http://blog.stevensanderson.com/2008/07/04/options-for-deploying-aspnet-mvc-to-iis-6/
Problem in IIS 7
In IIS 7, the .NET pipeline is fully integrated into IIS but by default, IIS does not serve static resources through your ASP.NET application. So your events will not fire for these types of resources.
IIS 7 Solution
The easy solution in IIS 7 is to add a setting in your web.config file to tell IIS to process all requests through your Global.asax events. Just add or change this section in your web.config to enable requests:
<system.webServer> <modules runAllManagedModulesForAllRequests="true" /> </system.webServer>
Popular Articles
Recent comments
- thank you for sharing
9 hours 40 min ago - Great explanation and more questions
1 day 12 hours ago - Insertion of illegal Element:
4 weeks 4 days ago - Insertion of illegal Element: 32
4 weeks 4 days ago - re "But, this will NOT work."
5 weeks 4 days ago - Unable to cast COM object of t
5 weeks 5 days ago - Saved my life
5 weeks 5 days ago - nice
8 weeks 4 days ago - good article
9 weeks 6 days ago - windows 2008 server backups
11 weeks 4 days ago
Similar
- Fixing Relative Paths in C# ASP.NET When Using Url Rewriting
- Getting Started with .NET Entity Framework
- Changing the Application Pool Identity with Windows Server 2008 and IIS 7
- How to Highlight the Day in the ASP.NET Calendar Control with the SelectedDate Property
- Create a Windows-style GroupBox in ASP.NET

How about iis 5.1
I running on iis 5.1, window XP,
I do as your guilde but cannot. script in .asax file still ignored.
Thank you
Hi friend,
your post was very useful to me. thank you very much..
Great! Thank you!
Hi,
Thanks a lot for that!
Regards
redM Software, Marcel
IIS Solution 7 does not work
The IIS 7 solution is not working...
thanks
Simply Superb....!
Thanks it helped me a lot
Thanks it helped me a lot
You saved the day.
Holy Toledo!
< system.webServer >
< modules runAllManagedModulesForAllRequests="true" / >
< /system.webServer >
THANK YOU.
I've been dying trying to get an old IIS6 (5.1) setup to work with IIS7.
I'm gonna put some key words here so others might this in the future in the scenario I was looking.
I had to add a space after the left bracket and before the right bracket to get it to post.
< system.webServer >
< handlers >
< add name="MyName" path="*.myext" verb="GET,HEAD,POST,DEBUG" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" allowPathInfo="true" / >
< /handlers >
< /system.webServer >
From what I've read (take it with a grain of salt), resourceType="Unspecified" is the equivalent of Verify that file exists. However, I needed it and the content of this article to get my setup t work.
From what I've read (take it with a grain of salt), resourceType="Unspecified" is the equivalent of "Verify that file exists" (from IIS6 (5.1)). However, I needed it and the content of this article to get my setup t work.