theringostarrs
theringostarrs

Reputation: 12390

HttpHandlers and HttpHandlerFactories

I would like to route incoming requests for different resources, some returning files such as css, others returning responses generated by the server, and others being redirected to aspx pages for AJAX functionality.

The current configuration uses an HttpModule to remap all requests to one handler, where urls are parsed and dispatched, as an entry point to the server.

I am wondering what the best configuration to handle requests would be, should I remap from the HttpModule to a HandlerFactory, where I parse the url, and route to an appropriate handler based on that url, or should I attempt to set it up all in the web.config?

Also How can I route requests to an aspx page from a HttpHandler and HttpHandlerFactory?

Upvotes: 1

Views: 1048

Answers (1)

Rex M
Rex M

Reputation: 144122

If the rules for routing requests to various handlers is very simple (e.g. ".ext goes to handler xyz") you should definitely use the built-in .NET mechanism with web.config - there's no reason to reinvent the wheel here. If the logic is possibly more complex or deals with more than just extension mappings, a module is appropriate.

Secondly, you should use an HttpHandlerFactory, because a factory can return IHttpHandlers, and an ASPX page is an IHttpHandler. So if you use your custom factory, if your logic determines it should "route" to an ASPX page, you can return an instance of the page directly from the Factory:

IHttpHandler thePage = PageParser.GetCompiledPageInstance(
    requestPath,
    pathToAspxFile,
    httpContext);

return thePage;

Upvotes: 1

Related Questions