Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding HttpHandler to MVC application

I have an MVC4 application to which I added an HttpHandler:

<system.web>
    ...
    <httpHandlers>
        <add path="Files" verb="*" type="MyHttpHandler" />
    </httpHandlers>
</system.web>

I also ignored the relevant path in RegisterRoutes so that the requests to "Files" are not handled by MVC:

routes.IgnoreRoute("Files/{*pathInfo}");

The problem is that the MyHttpHandler is invoked only for requests to "Files", not for any of its children or sub-folders.

I've tried using the <location> element, but getting that to work means that you will be hard coding the application's virtual path in the "path" attribute (e.g., <location path='MyApp\Files'>).

What is the correct method to use to allow all requests for "Files" and any of its sub-folders (and sub-folders of those folder, etc) to get routed to MyHttpHandler?

like image 388
Dan Avatar asked Oct 03 '22 12:10

Dan


1 Answers

Scratch that...<location> seems to work OK. Though, you need both the <web> and <webServer> entries to ensure that it works for both IIS and the Visual Studio Development Server. For example:

<location path="Files">`
    <system.webServer>
        <handlers>
            <add name="MyHandler" path="*" verb="*" type="MyHttpHandler" />
        </handlers>
    </system.webServer>
    <system.web>
        <httpHandlers>
            <add path="*" verb="*" type="MyHttpHandler" />
        </httpHandlers>
    </system.web>
</location>
like image 134
Dan Avatar answered Oct 05 '22 02:10

Dan