We have a Web Forms application that uses ASP.Net URL Rewrite 2.0. The following rewrite rule is configured to enable access to certain pages without specifying the .ashx
extension:
<rule name="RewriteASHX">
<match url="(customers|orders|products.*)"/>
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
</conditions>
<action type="Rewrite" url="{R:1}.ashx"/>
</rule>
Using Web API, we want to gradually replace our Web Forms-based API pages with Web API controllers. With URL Rewrite installed, only controllers matching the above rule will route correctly to the Web API controller. (Adding exclusions to the URL Rewrite rules seems to not let the request make it to the controller at all, giving a 404 result.)
The Web API controllers are set up in the following manner:
public class CustomersController : ApiController
{
[Route("api/v2/customers")]
[ResponseType(typeof(CustomerCollection))]
public IHttpActionResult Get()
{
...
}
[Route("api/v2/customers/{identifier}")]
[ResponseType(typeof(Customer))]
public IHttpActionResult Get(string identifier)
{
...
}
}
As described above, we have only been able to get Web API requests to route if they match a URL Rewrite condition.
The parameterless
api/v2/customers
inCustomerController
works because it matches<match url="(customers|orders|products.*)"/>
, butapi/v2/customers/C123
fails because it does not match the rewrite condition.
What is the best way to configure URL Rewrite to allow Web API requests to route properly?