asp.net-mvc - 使用默认 {controller}/{id} 路由和 {controller}/{action}/{id} 路由时与路由冲突
问题描述
我有以下路线设置;
RouteTable.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional });
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = System.Web.Http.RouteParameter.Optional },
constraints: null,
handler: new WebApiMessageHandler(GlobalConfiguration.Configuration));
以及以下控制器设置;
public class GetFileController : ApiController
{
[HttpGet]
public HttpResponseMessage Get(string id)
{
return Request.CreateResponse(HttpStatusCode.OK);
}
}
我在这里遇到的问题是这个网址
/api/GetFile/id_is_a_string
返回此错误;
<Error>
<Message>
No HTTP resource was found that matches the request URI /api/GetFile/id_is_a_string.
</Message>
<MessageDetail>
No action was found on the controller 'GetFile' that matches the name 'id_is_a_string'.
</MessageDetail>
</Error>
有没有办法让它不认为字符串参数实际上是动作名称?
我知道我可以将我的请求 URL 更改为;
/api/GetFile?id=id_is_a_string
但是这种路由更改会影响我已经设置的许多其他控制器,并且真的不希望通过一切来切换它以以这种方式发送请求。
如果我重新排序路由,它似乎可以正常工作,但是对于我理想情况下希望在其中包含多个端点的新控制器,我会收到此错误;
ExceptionMessage=Multiple actions were found that match the request:
新控制器
public class GettingThingsController : ApiController
{
[HttpPost]
public IHttpActionResult GetPeople()
{
return Ok();
}
[HttpPost]
public IHttpActionResult GetProducts()
{
return Ok();
}
}
反正有没有达到我所需要的?!
解决方案
您可以尝试使用 Regex 指定参数:
routeTemplate: "api/{controller}/{action:regex([a-z]{1}[a-z0-9_]+)}/{id:regex([0-9]+)}",
routeTemplate: "api/{controller}/{id:regex([0-9]+)}",
这些正则表达式在Route
属性中起作用。您可以在RouteTable
映射中对其进行测试。
推荐阅读
- python - 在 GPIO.RPi 回调事件中中断 while 循环
- mysql - 如何从表(sql)以这种形式输出到控制台?
- c# - wpf 丢失 noob - BindingExpression 路径错误 tih View binding
- css - 以编程方式将自定义 css 设计标记添加到 material-ui 组件
- java - QuerDSL 生成本机 Oracle 查询
- python - 在 Python 中为 PCA 绘制 y 轴上的方差比例
- apache-flink - Flink 需要 hive conf 目录的本地路径,但是如果我们在 yarn 上提交 flink 作业,如何提供该路径?
- node.js - 在 docker 容器上运行 nodejs 应用程序会给出“错误:找不到模块'/usr/src/app/nodemon'”
- migration - jboss 4.2.2 GA 与 java 11 兼容吗?
- javascript - 将 PHP 变量传递给 Javascript 变量不起作用