两个不同控制器的Web Api路由冲突? [英] Web Api Routing conflict for two different controllers?

查看:103
本文介绍了两个不同控制器的Web Api路由冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我正在将ASP.NET Web API 2与属性路由一起使用.我有两个控制器,它们的路由略有不同:

[RoutePrefix("api/menus")]
public class MenusController : ApiController {
}

[RoutePrefix("api/menus/items")]
public class MenuItemsController : ApiController {
}

两个控制器中都有所有CURD方法.现在,除了POST之外,所有方法都可以正常工作.每当我尝试调用MenuItemsControllerPOST方法时,都会出现以下错误:

Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL.

有趣的是,MenusControllerPOST方法工作正常.导致问题的仅是MenuItemsControllerPOST方法. 我也尝试使用RouteOrder属性,但是徒劳.

我们非常感谢您的帮助.

我的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆