【发布时间】:2016-04-15 12:13:49
【问题描述】:
我有默认的 webapi 路由配置:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional },
);
我想支持以下场景:
//api/mycontroller
public IQueryable<MyDTO> Get();
//api/mycontroller/{id} where id can be anything except "customaction1" and "customaction2"
public HttpResponseMessage Get(string id);
//api/mycontroller/customaction
[HttpPost]
public void CustomAction1([FromBody] string data);
[HttpPost]
public void CustomAction2([FromBody] string data);
我尝试将[Route("api/mycontroller/customaction1")] 应用于CustomAction1 方法,类似于CustomAction2,但得到:
找到多个与请求匹配的操作: MyProject.WebApiService.MyController 类型上的 CustomAction1 MyProject.WebApiService.MyController 类型上的 CustomAction2
【问题讨论】:
-
你配置属性路由了吗?
config.MapHttpAttributeRoutes() -
不。尝试过并且有效。我会接受的是asnwer。但是可以配置路由,使其默认使用没有属性的自定义操作?
-
您必须创建更多路线
标签: c# asp.net-web-api asp.net-mvc-routing url-routing