Search code examples
asp.netiishttpmodule

Can I stop ASP.NET from returning 'The resource cannot be found.'?


I have installed an HttpModule into my web app that will handle all requests with a given file extension.

I want ASP.NET to handle all requests with the extension, regardless of whether there is an underlying file on disk. So, when I added the extension to the 'Application Extension Mappings', I unchecked the 'Verify that file exists' checkbox.

However, this just transfers the file check to ASP.NET rather IIS, so I just get a different error page when requesting URLs with the file extension.

Is there a way to preempt this ASP.NET file checking and intercept the requests?


Solution

  • You need to create an HttpHandler instead of an HttpModule. Add the HttpHanlder extension to your web config and then all requests to the server for that extension will be directed to your handler.

    <httpHandlers>
        <add verb="*" path="*.ext" type="Domain.Model.MyHttpHandler" />
    </httpHandlers>
    

    The handler can then serve up whatever it wants. If it's an unknown filetype you will need to advise IIS of the type and that it is handled by ASP.NET.

    Here is a nice quick start guide