C# WebApi路由:同时使用api/{controller}/{id}和api/{controller}/{action}

C# WebApi路由:同时使用api/{controller}/{id}和api/{controller}/{action},c#,asp.net-web-api,asp.net-mvc-routing,url-routing,C#,Asp.net Web Api,Asp.net Mvc Routing,Url Routing,我有默认的webapi路由配置: config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional }, ); 我想支持以下场景: //api/mycontroller public IQueryable<MyDTO> Get(); //api/mycontr

我有默认的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);
//api/mycontroller
公共IQueryable Get();
//api/mycontroller/{id},其中id可以是除“customaction1”和“customaction2”之外的任何内容
公共HttpResponseMessage获取(字符串id);
//api/mycontroller/customaction
[HttpPost]
public void CustomAction1([FromBody]字符串数据);
[HttpPost]
public void CustomAction2([FromBody]字符串数据);
我已尝试将
[Route(“api/mycontroller/customaction1”)]
应用于
customaction1
方法,类似于
CustomAction2
但得到:

找到多个与请求匹配的操作: MyProject.WebApiService.MyController类型上的CustomAction1 MyProject.WebApiService.MyController类型上的CustomAction2


确保已配置属性路由以及默认配置

//....
config.MapHttpAttributeRoutes()

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional },
);
//....
如果要对out属性路由执行相同的操作,则需要显式配置路由

//This one constrains id to be an int
config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { action="Get", id = RouteParameter.Optional },
    constraints : new { id = @"\d" }
);
//This one should catch the routes with actions included
config.Routes.MapHttpRoute(
    name: "ActionRoutes",
    routeTemplate: "api/{controller}/{action}"
);

确保已配置属性路由以及默认配置

//....
config.MapHttpAttributeRoutes()

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional },
);
//....
如果要对out属性路由执行相同的操作,则需要显式配置路由

//This one constrains id to be an int
config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { action="Get", id = RouteParameter.Optional },
    constraints : new { id = @"\d" }
);
//This one should catch the routes with actions included
config.Routes.MapHttpRoute(
    name: "ActionRoutes",
    routeTemplate: "api/{controller}/{action}"
);

您是否配置了属性路由<代码>配置.MapHttpAttribute路由()。努力工作。我会接受的。但是,可以配置路由,使其在默认情况下与没有属性的自定义操作一起工作?您必须创建更多路由您配置了属性路由吗<代码>配置.MapHttpAttribute路由()。努力工作。我会接受的。但是,可以配置路由,使其在默认情况下与没有属性的自定义操作一起工作?您必须创建更多路由。在我的情况下,id是除操作名称之外的任何字符串。哦,我错过了这一点。我以为那是一个
int
。这确实改变了一切。我将不得不重新检查,在我的例子中,id是除操作名称之外的任何字符串哦,我错过了这个。我以为那是一个
int
。这确实改变了情况。我必须重新检查