views:

273

answers:

1

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?

A: 

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;
Rex M
The url scheme is complex/ mixture of file extensions and resource segments. Should the url parsing take place in the HttpModule or in the HttpHandlerFactory when the request is remapped to the factory?
theringostarrs
It depends. HttpModules are supposed to be orthogonal to HttpHandlers, so if the only purpose of your Module is to catch the request early and re-direct to the desired handler or factory, I would say go ahead and move all of that logic into your HttpHandlerFactory.
Rex M
I have decided I want to remap all non-file extension requests to the handler factory and specify httphandlers in the web.config for those(.css, .js). Does this fit? Will additional handlers work with the httpmodule mapping on a wildcard?
theringostarrs
@jimoih I'm afraid I can't answer without more clarification. Do make sure that all requests in IIS are set to be handled by the ASP.NET engine - otherwise requests for static files like .css will bypass ASP.NET and be served directly from the filesystem.
Rex M