当我尝试有2个“Get”方法时,我一直得到这个错误
发现了多个与请求:webapi匹配的操作
我一直在看其他类似的问题,但我不明白。
我有2个不同的名字,并使用“HttpGet”属性
[HttpGet]
public HttpResponseMessage Summary(MyVm vm)
{
return null;
}
[HttpGet]
public HttpResponseMessage FullDetails()
{
return null;
}
当我尝试有2个“Get”方法时,我一直得到这个错误
发现了多个与请求:webapi匹配的操作
我一直在看其他类似的问题,但我不明白。
我有2个不同的名字,并使用“HttpGet”属性
[HttpGet]
public HttpResponseMessage Summary(MyVm vm)
{
return null;
}
[HttpGet]
public HttpResponseMessage FullDetails()
{
return null;
}
当前回答
确保你没有装饰你的控制器方法默认的GET|PUT|POST|DELETE动作与[HttpPost/ PUT / GET / DELETE]属性。我已经将此属性添加到我的香草后控制器动作,它导致404。
希望这能帮助到一些人,因为它可能会非常令人沮丧,并导致进展停滞。
其他回答
你可以添加[Route("api/[controller]/[action]")]到你的控制器类中。
[Route("api/[controller]/[action]")]
[ApiController]
public class MySuperController : ControllerBase
{
...
}
由于使用MVC控制器而不是Web API控制器,这是可能的。 检查Web API控制器中的命名空间,它应该如下所示
using System.Net;
using System.Net.Http;
using System.Web.Http;
如果命名空间如下所示,则在web api控制器方法调用时给出上述错误
using System.Web;
using System.Web.Mvc;
例如=> TestController
[HttpGet]
public string TestMethod(int arg0)
{
return "";
}
[HttpGet]
public string TestMethod2(string arg0)
{
return "";
}
[HttpGet]
public string TestMethod3(int arg0,string arg1)
{
return "";
}
如果只能更改WebApiConfig.cs文件。
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/",
defaults: null
);
就是这样。
结果:
有可能你的webmethods被解析为相同的url。请浏览以下连结:-
http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api
所以,你可能需要将你的methodname添加到路由表中。
我知道这是一个老问题,但有时,当你使用AngularJS中的服务资源连接到WebAPI时,确保你使用了正确的路由,否则会发生这个错误。