Unknown
Unknown

Reputation:

StatusCode in IHttpModule is always 200?

I have an custom IHttpModule that handels all available events and logs the HttpContext.Current.Response.StatusCode to a file.

My web.config does not contain any other module in <httpModules> so all errors are promoted to the browser.

Although the Browsers shows a 404, the log file is full of 200 (Ok) entries and not a single 404.

What am I missing?


Update

An Url ending with .aspx does have a 404 StatusCode in the PreSendRequestHeaders event, but Urls wending with .pdf for example show StatusCode 200 in all events?!

Upvotes: 2

Views: 1128

Answers (3)

MichaelD
MichaelD

Reputation: 8767

A bit late to the party. Try removing preCondition="managedHandler" in web.config for your module

Upvotes: 0

Pedro
Pedro

Reputation: 3781

I had the same problem and I resolved it casting the current exception in the handler in HttpException. Then, you can get the Http error code.

Exception exception = HttpContext.Current.Server.GetLastError();
HttpException httpException = exception as HttpException;
if (httpException != null)
{
    int httpCode = httpException.GetHttpCode();
}

Source : http://www.digitallycreated.net/Blog/57/getting-the-correct-http-status-codes-out-of-asp.net-custom-error-pages

Hope it helps :)

Upvotes: 0

Zhaph - Ben Duguid
Zhaph - Ben Duguid

Reputation: 26956

Is your module actually running when there's a 404?

Have you attached a debugger to the application with a breakpoint in the module - does it get hit at that when you request a non-existant .aspx page? Does it get hit when you request a non-existant file or directory?

Depedning on the set-up of IIS, it's possible that ASP.NET isn't getting a look in with these errors - it's not going to handle a request for somepage.html for example, or missing.gif.

Upvotes: 0

Related Questions