Reputation: 21
I am creating a REST service using .NET and have used a generic handler, Default.ashx, to handle the incoming request. This allows me to access the service using methods like "http://rest/test" without issue. But when a file extension is added IIS no longer redirects the request but instead looks for a file. How can the web.config be modified so that URL request like "http://foo/test.xml" and "http://foo/test.json" are also handled by the DefaultHandler.ashx? I have done this before so know its possible but cannot remember the configuration.
Upvotes: 2
Views: 731
Reputation: 64248
You can see an example of this in my HttpClone app's web.config. The gist of it involves removing handlers for the extensions you don't want like this:
<system.webServer>
...
<handlers accessPolicy="Read, Script">
<clear />
<add name="Favorite-Icon" path="/favicon.ico" verb="GET,HEAD" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="HttpClone" path="*" verb="GET,HEAD,POST,DEBUG" type="Namespace.MyCustomHandler, AssemblyName" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode" />
</handlers>
This says to use a custom handler for all requests for all URIs except '/favicon.ico'. If you still need to use the 'Default.ashx' then you will need to get handler for the ashx type and map it to handle all paths just like this. Generally though there is no need for the ashx extension handler, just implement IHttpHandler in any assembly and reference it in the 'type' attribute above.
Note that the cassini web server (the test server in VStudio) will not map the default directory '/' to you're handler. To fix this for cassini you need a default.aspx document to exist (although it can be empty).
Note 2 - The configuration above is for integrated mode only, for classic mode the concept is the same but the settings are in a different location.
Upvotes: 1