多个动作被发现匹配请求的Web API是什么?

编程入门 行业动态 更新时间:2024-10-16 20:25:58
本文介绍了多个动作被发现匹配请求的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是什么?

本文发布于:2023-11-02 17:02:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552932.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   动作   发现   API   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!