我在c#中實現了web API MVC。我的片斷實現: - WebApiConfig.csWEB API路由錯誤發現多個操作使請求與不同的API路徑匹配
config.Routes.MapHttpRoute(
name: "getMultiProbe",
routeTemplate: "api/v1/{controller}/probe/{server}"
);
config.Routes.MapHttpRoute(
name: "getCurrentMultiProbe",
routeTemplate: "api/v1/{controller}/currentmultiprobe/{server}"
);
而與產生該問題的方法,相關的控制器是: - 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;
}
從靜止客戶端返回的錯誤:
{"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()" }
我不必匹配不同的路徑,因爲/ currentmultiprobe和/ probe上的路徑不同。我試圖改變路徑和服務工作之間的名稱輸入參數。我問是否有一種方法來處理這種配置。
用它在你的控制器這將映射到任何其他控制器?如果不是,則使用默認值縮小範圍。 – Nkosi