WEB API 路由错误发现多个操作与具有不同 API 路径的请求相匹配

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

我正在用 C# 实现 Web API MVC。我的片段实现是: - WebApiConfig.cs

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;
}

来自其余客户端 return 错误:

{"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 上的路径不同。我试图更改路径和服务之间的名称输入参数。请问这个配置有没有办法。

OP 错误的原因是路由 table 无法根据模板中的路由参数区分两个操作,并且两个操作具有相同的 HTTP 方法(POST)

映射时使用defaults参数缩小映射(路线)。

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" }
);

您可以将一个路由声明与{action}一起使用

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

并以这种方式在您的控制器中使用它

[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;
}