gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:57:10 28 4
gpt4 key购买 nike

我正在用 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}"
);

与生成问题的方法关联的 Controller 是: - 组织 Controller .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;
}

从 rest 客户端返回错误:

{"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中错误的原因是路由表无法根据模板中的路由参数区分两个 Action ,并且两个 Action 具有相同的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" }
);

关于c# - WEB API 路由错误发现多个操作与具有不同 API 路径的请求相匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42110446/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com