How to bypass the exception multiple actions were found in ASP.NET Web API

杀马特。学长 韩版系。学妹 提交于 2019-12-05 16:13:40

First of all, it shouldn't be possible for those route to match Active action unless you apply HttpGet attribute on it. I will assume you already did that. The second thing is that what you want is not logical. You would like to make a get request and have two identical actions. You are now using so-called HTTP-method-based routing. For you situation, I am sure that so-called action-based routing is the best.

Assuming you have following two actions:

[HttpGet]
public IEnumerable<String> Active()
{
    var result = new List<string> { "active1", "active2" };
    return result;
}

[HttpGet]
public IEnumerable<string> Retrieve()
{
    return new[] { "value1", "value2" };
}

You route should be as follows:

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

Then you can request to those with following URIs:

GET /controllername/retrieve

GET /controllername/active

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!