【发布时间】:2018-02-08 14:36:11
【问题描述】:
我们有一个使用 ASP.Net URL Rewrite 2.0 的 Web 窗体应用程序。以下重写规则配置为无需指定 .ashx 扩展即可访问某些页面:
<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>
使用 Web API,我们希望逐渐将基于 Web 表单的 API 页面替换为 Web API 控制器。安装 URL Rewrite 后,只有符合上述规则的控制器才能正确路由到 Web API 控制器。 (向 URL 重写规则添加排除项似乎根本不允许请求到达控制器,从而给出 404 结果。)
Web API 控制器按以下方式设置:
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)
{
...
}
}
如上所述,我们只有在 Web API 请求与 URL 重写条件匹配时才能获取路由。
CustomerController中的无参数api/v2/customers有效,因为它匹配<match url="(customers|orders|products.*)"/>,但api/v2/customers/C123失败,因为它不匹配重写条件。
配置 URL 重写以允许 Web API 请求正确路由的最佳方法是什么?
【问题讨论】:
标签: c# asp.net url-rewriting webforms asp.net-web-api2