找到多个与请求 Web API 匹配的操作? [英] Multiple actions were found that match the request Web API?

查看:23
本文介绍了找到多个与请求 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 匹配的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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