1

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?

TecMan
  • 2,743
  • 2
  • 30
  • 64

2 Answers2

0

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)

frenchone
  • 1,547
  • 4
  • 19
  • 34
-1

You can try;

if (Request.Path.ToLowerInvariant().IndexOf(".aspx") > -1)
{
    // static files
}
Ömer Faruk Aplak
  • 889
  • 2
  • 9
  • 21
  • 1
    No, not this. I see I need to clarify the point. We need to avoid the calling of Application_BeginRequest for static resources by the platform itself to have the maximum performance. – TecMan May 15 '13 at 09:18
  • This is buggy (and slow code) - there are many more extensions that are not static files and pass from Begin Requests. It is buggy because the `.aspx` as you search it may exist and in non aspx file ! Also you search it from the start, but its on the end of the file (slow code). And is not what the OP ask for. – Aristos May 15 '13 at 11:45