2017-02-08 106 views
4

我在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上的路徑不同。我試圖改變路徑和服務工作之間的名稱輸入參數。我問是否有一種方法來處理這種配置。

+0

用它在你的控制器這將映射到任何其他控制器?如果不是,則使用默認值縮小範圍。 – Nkosi

回答

4

原因在OP的錯誤是基於模板,這兩個動作的路由參數具有相同的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" } 
); 
+0

它的工作原理!大 – Stefano

2

您可以使用{}行動

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; 
} 
相關問題