简体   繁体   中英

WEB API Routing Error Multiple actions were found that match the request with different API paths

I'm implementing web API MVC in c#. My snippet implementation is: - WebApiConfig.cs

config.Routes.MapHttpRoute(
   name: "getMultiProbe",
   routeTemplate: "api/v1/{controller}/probe/{server}"
);

config.Routes.MapHttpRoute(
   name: "getCurrentMultiProbe",
   routeTemplate: "api/v1/{controller}/currentmultiprobe/{server}"
);

And controller associated with the methods that generate the issue are: - HistController.cs

[HttpPost]
public Dictionary<string, List<DataSample>> getMultiProbe(string server, [FromBody] Dictionary<string,Object> request)
{       
    Debug.WriteLine("ENTER [GetMultiProbe] "+ request["from"] + " -   mode: " + request["mode"]);
    string[] tagnames = (string [])request["tagnames"];
    return null;        
}

[HttpPost]
public Dictionary<string, Object[]> getCurrentMultiProbe(string server, [FromBody] String[] tagnames)
{       
    Debug.WriteLine("ENTER [getCurrentMultiProbe] server: " + server + " - tagnames: " + tagnames);
    return null;
}

from rest client return the error:

{"Message": "An error has occurred.","ExceptionMessage": "Multiple actions were found that match the request: getMultiProbe on type HistService.Controllers.HistController getCurrentMultiProbe on type HistService.Controllers.HistController", "ExceptionType": "System.InvalidOperationException", "StackTrace": " at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext) at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext) at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken) at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()" }

I wouldn't have to match the different paths, because the paths differ on /currentmultiprobe and /probe. I tried to change the name input parameter between paths and the service works. I ask if there is a way to work this configuration.

The reason for the error in OP is that the routing table could not differentiate between the two action based on the route parameters in the template and that both action have the same HTTP Method (POST)

Narrow the mapping (route) by using defaults parameter when mapping.

config.Routes.MapHttpRoute(
   name: "getMultiProbe",
   routeTemplate: "api/v1/{controller}/probe/{server}",
   defaults: { controller = "Hist", action = "getMultiProbe" }
);

config.Routes.MapHttpRoute(
   name: "getCurrentMultiProbe",
   routeTemplate: "api/v1/{controller}/currentmultiprobe/{server}",
   defaults: { controller = "Hist", action = "getCurrentMultiProbe" }
);

You can use one route declaration with {action}

config.Routes.MapHttpRoute(
       name: "ActionRoute",
       routeTemplate: "api/v1/{controller}/{action}/{server}"
    );

and use it in your controller in that way

[HttpPost]
[ActionName("probe")]
public Dictionary<string, List<DataSample>> getMultiProbe(string server, [FromBody] Dictionary<string,Object> request)
{       
    Debug.WriteLine("ENTER [GetMultiProbe] "+ request["from"] + " -   mode: " + request["mode"]);
    string[] tagnames = (string [])request["tagnames"];
    return null;        
}

[HttpPost]
[ActionName("currentmultiprobe")]
public Dictionary<string, Object[]> getCurrentMultiProbe(string server, [FromBody] String[] tagnames)
{       
    Debug.WriteLine("ENTER [getCurrentMultiProbe] server: " + server + " - tagnames: " + tagnames);
    return null;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM