问题
I want handle the requests to my application "http://example.com/whateverpath" by a custom HttpHandler but the return things depending of the value of "whateverpath".
So users accessing "http://example.com/path1" will get different response than users accessing "http://example.com/path2", but both request must be handled in the same HttpHandler. The idea is find "whateverpath" in a database and depending of the result, return the response content.
I hear about URL routing and I already have a custom Http handler working, but can I combine both technique to get what I need?
I will appreciate any comment respect this issue.
Cheers Frank Abel
回答1:
So you have a class that implements IHttpHandler
called: MyHandler
and it's in the namespace Example
, you need to make the following entries in the site's Web.Config
in the httpHandlers section:
<httpHandlers>
<add verb="*" path="*" type="Example.MyHandler"/>
</httpHandlers>
Since this redirects all URLs for your web site/application to your handler you have to consider how to serve static content (imgs, scripts, style sheets etc). One way is to store such static content in a consistent URL like http://example.com/static/...
, you can then set your handlers as such:
<httpHandlers>
<add verb="*" path="*" type="Example.MyHandler"/>
<add verb="GET,HEAD" path="static/*" type="System.Web.StaticFileHandler" />
</httpHandlers>
For your local dev webserver (embedded in Visual Studio) this is all that's needed. For IIS, you also need to tell IIS how to deal with these URLS (since the server first analyses a request to decide where to send it - including whether to send it to ASP.NET or some other extension).
- Open: IIS Manager ->
- Section: Websites ->
- Right click on your website ->
- Option: Properties ->
- Tab: Home Directoy ->
- Button: [Configuration...] ->
- Tab: Mappings ->
- Section: "Wildcard application maps (order of implementation):" ->
- Button: [Insert...] ->
- Executable: "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" (or whichever version of the .NET runtime your handler uses) ->
- Uncheck "Verify that file exists" ->
- Button: [OK]
Now both IIS and ASP.NET know how to deal with your URLS.
The above approach means when requesting static files, ASP.NET is actually serving the files, not IIS - which leads to a few disadvantages (discussed here). You can override this behaviour (disable wildcard mapping from the static directory) by switching the directory to an application (in IIS Manager), removing the wildcard mapping statement (added above) and switching it back from an application. Voilà - static files are handled by IIS without pestering your ASP.NET.
回答2:
I do not recommend combining URL routing and HTTP handlers.
This seems like a perfect job for URL routing. However, I would not use an HTTP handler for it.
Simply map "~/CustomData/whateverpath" to an ASPX page. Then have the page load the data from the database. Afterall, if the logic to look up the data is the same no matter what "whateverpath" is, you don't want to repeat your logic for every variation. Instead, you want to map it to a single file that will load the correct data for all cases.
HTTP handlers are a completely different matter and should not be used for this purpose. (BTW, I just published an article on HTTP handlers. You can view it at http://www.blackbeltcoder.com/Articles/asp/writing-a-custom-http-handler-in-asp-net).
回答3:
First off, I would concur with the previous post by Jonathan Wood, that using routing within HttpHandler isn't a good idea. But I am pretty sure he was referring to the standard MVC routing there.
A good approach would be in using custom routing. I published an article about it - Basic Routing for HttpHandler
来源:https://stackoverflow.com/questions/4320247/asp-net-custom-httphandler-and-url-routing