多个动作被发现匹配请求的Web API是什么? [英] Multiple actions were found that match the request Web API?

查看:335
本文介绍了多个动作被发现匹配请求的Web API是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的Web API,我在这个新的。我被困在一个路由问题。我有以下操作的控制器:

  //获取API /典礼
    公共IEnumerable的<典礼及GT; GetCeremonies()
    {
        返回db.Ceremonies.AsEnumerable();
    }    //获取API /典礼/ 5
    公开仪式GetCeremony(INT ID)
    {
        典礼仪式= db.Ceremonies.Find(ID);
        回归仪式;
    }    公共IEnumerable的<典礼及GT; GetFilteredCeremonies(搜索过滤器)
    {
        返回filter.Ceremonies();
    }

问题occure当我加入了行动 GetFilteredCeremonies 我的控制器。增加这个当我使一个Ajax调用 GetCeremonies 行动,那么它返回与以下消息的异常之后:

 消息:发生错误,ExceptionMessage:多的行动是
 找到相匹配的要求,即

FYI:参数搜索是模型类,它包含的属性和功能名称仪式

修改

路线:

  config.Routes.MapHttpRoute(
                名称:DefaultApi
                routeTemplate:API / {}控制器/(编号),
                默认:新{ID = RouteParameter.Optional}
            );


解决方案

如果你不要求必然要使用REST服务,使用 API / {控制器} / {ID} 路线和尝试的基础上的方法来解决行动获取 / 发表 / 删除 / ,您可以修改你的路线,经典的MVC路线 API / {控制器} / {行动} / {ID} ,它会解决你的问题。

I am using web API and i am new in this. I am stuck in a routing problem. I have a controller with following actions :

    // GET api/Ceremony
    public IEnumerable<Ceremony> GetCeremonies()
    {
        return db.Ceremonies.AsEnumerable();
    }

    // GET api/Ceremony/5
    public Ceremony GetCeremony(int id)
    {
        Ceremony ceremony = db.Ceremonies.Find(id);
        return ceremony;
    }

    public IEnumerable<Ceremony> GetFilteredCeremonies(Search filter)
    {
        return filter.Ceremonies();
    }

The problem occure when i added the action GetFilteredCeremonies to my controller. After adding this when i make an ajax call to GetCeremonies action then it return an Exception with following message :

"Message":"An error has occurred.","ExceptionMessage":"Multiple actions were 
 found that match the request

FYI: The parameter Search is the Model class which contains properties and a function name Ceremonies.

EDIT

Route:

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

解决方案

If you're not requirement bound to use REST services that use api/{controller}/{id} route and attempt to resolve action based on method GET/POST/DELETE/PUT, you can modify your route to classic MVC route to api/{controller}/{action}/{id} and it will solve your problems.

这篇关于多个动作被发现匹配请求的Web API是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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