两个不同控制器的Web Api路由冲突? [英] Web Api Routing conflict for two different controllers?
问题描述
好吧,我正在将ASP.NET Web API 2与属性路由一起使用.我有两个控制器,它们的路由略有不同:
[RoutePrefix("api/menus")]
public class MenusController : ApiController {
}
[RoutePrefix("api/menus/items")]
public class MenuItemsController : ApiController {
}
两个控制器中都有所有CURD方法.现在,除了POST
之外,所有方法都可以正常工作.每当我尝试调用MenuItemsController
的POST
方法时,都会出现以下错误:
Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL.
MenusController
的POST
方法工作正常.导致问题的仅是MenuItemsController
的POST
方法.
我也尝试使用Route
的Order
属性,但是徒劳.
我们非常感谢您的帮助.
我的POST
方法的路线如下:
-
POST api/menus/
-
POST api/menus/items
Aneeq 尝试同时加入两个控制器:
[RoutePrefix("api/menus")]
public class MenusController : ApiController
{
[Route("")]
public string Get()
{
return "menus";
}
}
[RoutePrefix("api/menus")]
public class MenuItemsController : ApiController
{
[Route("items")]
public string Get()
{
return "menus items";
}
}
我已经测试过并且工作正常.酷吗?
PS:我使用获取"只是为了简化解决方案.
Well, I am using ASP.NET Web API 2 with attribute routing. I have two controllers with minor difference in their routes:
[RoutePrefix("api/menus")]
public class MenusController : ApiController {
}
[RoutePrefix("api/menus/items")]
public class MenuItemsController : ApiController {
}
I have all the CURD methods in both controllers. Now all the methods are working working fine except for the POST
. Whenever I try to call POST
method of MenuItemsController
, I get the following error:
Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL.
Interestingly, POST
method of MenusController
works perfectly OK. It's only MenuItemsController
's POST
method that's causing problem.
I tried using Order
property of Route
as well, but in vain.
Any help is highly appreciated.
EDIT:
My POST
methods' routes are below:
POST api/menus/
POST api/menus/items
Aneeq try to join both controllers:
[RoutePrefix("api/menus")]
public class MenusController : ApiController
{
[Route("")]
public string Get()
{
return "menus";
}
}
[RoutePrefix("api/menus")]
public class MenuItemsController : ApiController
{
[Route("items")]
public string Get()
{
return "menus items";
}
}
I have tested and works fine. Is it cool?
PS: I used "Get" just to simplify the solution.
这篇关于两个不同控制器的Web Api路由冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!