Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application_BeginRequest not fired for JPG on production server

Tags:

c#

.net

iis

I have an IIS 7.5 website running .NET 4.0 in Classic pipeline mode.

I have created a simple default image setup where if an image is called but the physical file does not exist the request is redirected to a default image using the following code in the Application_BeginRequest event:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    if (Request.PhysicalPath.EndsWith(".jpg") && File.Exists(Request.PhysicalPath) == false)
    {
        Context.RewritePath("~/images/nophoto.jpg");
    }
}

This works fine on my VS2010 dev server but when on a production environment the Application_BeginRequest event is not called for JPG requests and all I get is the standard HTTP Error 404.0 - Not Found error.

I have tried setting the runAllManagedModulesForAllRequests option in the Web.Config to true but this does not appear to help:

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

From my understanding this should cause all requests to go through .NET and therefore trigger the Application_BeginRequest event?

Desired Outcome:

I'd like all requests to go through .NET so that the Application_BeginRequest event is called for JPGs and a default image is returned if no image is found.

like image 584
antfx Avatar asked Oct 22 '22 04:10

antfx


1 Answers

That won't happen with Classic mode, you need to switch to Integrated Mode.

This article might provide an insight.

like image 141
Tamim Al Manaseer Avatar answered Oct 29 '22 21:10

Tamim Al Manaseer