的WebAPI多行为被发现GETALL()和GetByIds(INT [] IDS) [英] WebApi Multiple actions were found with GetAll() and GetByIds(int[] ids)

查看:230
本文介绍了的WebAPI多行为被发现GETALL()和GetByIds(INT [] IDS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用标准的路线:

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

通过以​​下操作:

public class ValuesController : ApiController
{
    // GET api/values
    public string GetAll()
    {
        return "all";
    }

    // GET api/values/5
    public string GetById(int id)
    {
        return "single";
    }

    // GET api/values?ids=1&ids=2
    public string GetByIds([FromUri] int[] ids)
    {
        return "multiple";
    }

和做出请求的 / API /值后,我得到这个异​​常:

And make a request to /api/values, I get this exception:

Multiple actions were found that match the request: 
System.String GetAll() on type MvcApplication4.Controllers.ValuesController
System.String GetByIds(Int32[]) on type MvcApplication4.Controllers.ValuesController

我一直在纺纱我的车轮试图找到解决这个解决方案。这是我的信念,在 GETALL GetByIds 操作被视为在这里,但他们不是因为GetByIds有不同的签名。

I've been spinning my wheels trying to find a solution around this. It's my belief that the GetAll and GetByIds actions are considered Multiple here, but they aren't because the GetByIds has a different signature.

是否有这种不涉及添加一个变通 {行动} 来的路线?

Is there a work around for this that doesn't involve adding {action} to the route?

推荐答案

感谢您的输入每个人。围绕踢选项之后,我发现这样做的唯一途径,是对GETALL和GetByIds行动结合起来,并切换是IDS的长度。

Thanks for the input everyone. After kicking options around, the only way I found to do this, is to combine the GetAll and GetByIds action and switch case the length of ids.

public class ValuesController : ApiController
{
    // GET api/values/5
    public string GetById(int id)
    {
        return "single";
    }

    // GET api/values
    // GET api/values?ids=1&ids=2
    public string GetByIds([FromUri] int[] ids)
    {
        switch (ids.Length)
        {
            case 0:
                return "all";

            default:
                return "multiple";
        }
    }

这篇关于的WebAPI多行为被发现GETALL()和GetByIds(INT [] IDS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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