4

私はWeb API MVCをC#で実装しています。私のスニペットの実装は次のとおりです。 - 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におけるエラーの理由は、ルーティングテーブルが両方のアクションテンプレートおよびそのルートパラメータに基づいて(POST)

マッピング範囲を絞る同じHTTPメソッドを持つ2つのアクションを区別できなかったことですマッピング時に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}" 
    ); 

で1つのルート宣言を使用し、そのよう

[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; 
} 
関連する問題