Yucel
Yucel

Reputation: 2673

Static file requests don't raise application_error event

in my Asp.Net MVC project I'm cathing Http FileNotFound Exceptions (for a missing image) and then redirect request to a default image like below

  protected void Application_Error(object sender, EventArgs e)
    {
        if (Request.Path.StartsWith("/images/profile"))
        {

            Response.Redirect("/images/profile/default.jpg", true);
            return;
        }
    }

It is working in development envirement when I am debugging my website. But when I deploy it to production server that runs IIS 7.5 This code isn't working, request to a image file does not trigger the Application_Error event. Is there any configuration on IIS? I can't find the problem.

Upvotes: 0

Views: 676

Answers (1)

SLaks
SLaks

Reputation: 887305

You need to configure IIS to run all requests through ASP.Net.

Add <modules runAllManagedModulesForAllRequests="true" /> to <system.webServer> in Web.config.


Also, you should add a route for this instead of handling the Error event.

Upvotes: 1

Related Questions