Tecman
Tecman

Reputation: 3009

How to check whether Application_BeginRequest is called for static resources like images on real IIS webserver?

We have an ASP.NET 4.0 website, and we use the Application_BeginRequest event in Global.asax to do some smart redirects. When debugging the solution under the local ASP.NET Development Server provided by Visual Studio (no IIS), Application_BeginRequest is called for both apsx pages and the static resources like css files, jpg/gif images, etc our pages contain.

That's a known issue, but what about the real IIS hosting of our hosting provider (Windows 2008/IIS 7.0)? How can we check whether this happens for the static resources? And how to prohibit this?

Upvotes: 1

Views: 958

Answers (2)

frenchone
frenchone

Reputation: 1607

All requests will flow through Application_BeginRequest unless you tell the webserver to behave differently by setting runAllManagedModulesForAllRequests to false

 <system.webServer>
  <modules runAllManagedModulesForAllRequests="false" />
 </system.webServer>

If you don't have access to web.config then you can set up a quick test : publish two distincts images : redirect.jpg and noredirect.jpg and set a redirection in Application_BeginRequest and see if it occurs or not

var url = ((System.Web.HttpApplication)sender).Request.Url;
if (url.EndsWith("noredirect.jpg"))
{
 Response.Redirect(url.replace("noredirect.jpg","redirect.jpg"));
}

Then try to access "noredirect.jpg", if "redirect.jpg" shows instead then the redirect is in action ( = default setting)

Upvotes: 0

&#214;mer Faruk Aplak
&#214;mer Faruk Aplak

Reputation: 911

You can try;

if (Request.Path.ToLowerInvariant().IndexOf(".aspx") > -1)
{
    // static files
}

Upvotes: -1

Related Questions