开发者

HttpModule appears to fire for static pages only

I have an HttpModule for handling errors up and running in the visual studio development environment. It works well. To the client, there are hard 404s and no apparent redirects. Google Analytics should be very happy with this.

Debugging in Visual Studio 2008, when one navigates to localhost:

[port]/this_page_doesnt_exist.html, I get a our 404 error page and the hard 404.  

It also works on the development server for

localhost:[port]/this_page_doesnt_exist.aspx.

When I port it all over to our IIS7 web server, I've got it working exactly as it should for the static HTML files. However, I've determined that my module isn't getting called at all for aspx files. If I turn customErrors Off, I get the standard ugly 404 page. (If customErrors is RemoteOnly, these do kick in.)

The clues seem to be pointing at some IIS setting that I don't have properly tweaked, but I can't for the life of me find it. Any ideas?

More information:

I'm running in integrated mode and my web.config contains the following sections:

<configuration>
    <system.web>
      <httpModules>
        <add name="ErrorModule" type="HttpModules.ErrorModule, HttpModules"/>
      </httpModules>
    </system.web>
    <system.webServer>
      <validation validateIntegratedModeConfiguration="false"/>
      <modules>
        <add name="ErrorModule"  preCondition="managedHandler" type="HttpModules.ErrorModule, HttpModules"/>
      </modules>
    </system.webServer>
</configuration>

Yet More Information

What I thought was happening in IIS7 was not what was happening. The only reason I saw the 404 error message for HTML files on the server was that the CustomErrorHandler native module was getting it. 开发者_高级运维 When I turned that module off, I now get my ErrorHandler to run for aspx files, but not for HTML files.

I must be close, but I've struggled to get my settings the rest of the way. Can anyone push me over the goal line?


I'd wager you do not have the module properly registered in web.config for IIS 7 when using the integrated pipeline. To have the HTTP Module play nice with IIS 7 you need to also register the module in the <system.webServer> section like so:

<configuration>
    <system.web>
        <httpModules>
            <add name="..." type="..." />
        </httpModules>
    </system.web>

    <system.webServer>
            <validation validateIntegratedModeConfiguration="false" />

            <modules>
                <add name="..." type="..." preCondition="managedHandler" />
            </modules>
    </system.webServer>
</configuration>

I take it you have the <httpModules> section but not the <system.webServer> section?

On an aside, what error logging module are you using? If you are using your own home-baked one, might I suggest using ELMAH? It's easy to get started with, is open-source, and is widely used, so you can rest assured that the bugs and kinks are ironed out of it.

Happy Programming!


After quite a bit of struggle, my conclusion is that the answer is: "The interaction of existing modules on your particular web server, and how error handling is supported by your web.config and IIS settings, can lead to uncommon developer-specific complications. Thankfully, if you want to add an HttpModule for error handling, the vagaries of the problem are solved by open source tools, such as ELMAH". (I can't vouch for ELMAH, but I looked in the code, and it seems to need to do a lot more than what I was doing to get it to work.)

But if you've come this far, you deserve to know what I got to work:

In IIS Manager, I modified the Module order such that my error handler came before the native CustomErrorModule. (Or, respecify all your modules in the site root web.config in the order you want them.) Run your module only on managed code.

For some reason, my module doesn't notice requests for missing static pages, even though it is running for static pages. Consider that my unsolved problem (that, presumably, ELMAH does solve).

In IIS Manager, I set Error Pages to point to custom error pages. This is for the static pages. They were aspx files that set the Response.StatusCode. Voila! Hard error codes for everything.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜