tags2k
tags2k

Reputation: 84385

Getting correct Response.StatusCode from Application_OnError (ASP.NET MVC)

I'm trying to create some basic logging for unhandled exceptions in my ASP.NET MVC application, but as I'm using IIS6 all requests come through .NET. Now while I agree that missing images are an important thing to flag, they aren't show-stoppers and so I'd like to set the event priority to high for 404s unless they are images (which will get medium priority).

The problem is that in Application_OnError the Response.StatusCode is 200, but the final result sent out is 404. To this end, I don't seem to be able to watch out for 404s to set the appropriate priority.

Is there a way to tell what the StatusCode is going to be?

Upvotes: 1

Views: 1579

Answers (2)

veggerby
veggerby

Reputation: 9020

You can get it via Context.Server.GetLastError():

if ((Context.Server.GetLastError() is HttpException) && ((Context.Server.GetLastError() as HttpException).GetHttpCode() == 404))
{
    // do something
}

Upvotes: 6

Anthony Main
Anthony Main

Reputation: 6068

You would have to send all traffic through to the .net isapi layer and do a manual 404 check yourself (if file exists) I would imagine

Upvotes: 0

Related Questions