找到多个与请求 Web API 匹配的操作?

编程入门 行业动态 更新时间:2024-10-16 08:18:39
本文介绍了找到多个与请求 Web API 匹配的操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 Web API,我是新手.我陷入了路由问题.我有一个具有以下操作的控制器:

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(); }

当我将操作 GetFilteredCeremonies 添加到我的控制器时出现问题.添加此后,当我对 GetCeremonies 操作进行 ajax 调用时,它返回一个带有以下消息的异常:

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

仅供参考:参数 Search 是包含属性和函数名称 Ceremonies 的模型类.

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

编辑

路线:

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

推荐答案

如果您不是必须使用使用 api/{controller}/{id} 路由的 REST 服务并尝试根据方法GET/POST/DELETE/PUT解析动作,你可以将你的路由修改为经典的MVC路由到 api/{controller}/{action}/{id} 它将解决您的问题.

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:00:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552929.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   操作   Web   API

发布评论

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

>www.elefans.com

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