2016-11-25 11 views
2

私は以下のように異なるデータ型を持つ2つのPOSTメソッドを持つことができます -Web APIを複数のポスト方法は

[HttpPost]   
public HttpResponseMessage Post([StringBody]string data) 
{ 
    // Logic 
} 

[HttpPost] 
public HttpResponseMessage Post(Requirements objRequirement) 
{ 
    //Logic 
} 

私は郵便配達で、エラーの下に取得しています: -

<Error> 
    <Message>An error has occurred.</Message> 
    <ExceptionMessage>Multiple actions were found that match the request: 
System.Net.Http.HttpResponseMessage Post(Newtonsoft.Json.Linq.JObject) on type ATL.Trebuchet.RestApi.Controllers.ValuesController 
System.Net.Http.HttpResponseMessage Post(ATL.Trebuchet.RestApi.Models.Requirements) on type ATL.Trebuchet.RestApi.Controllers.ValuesController</ExceptionMessage> 
    <ExceptionType>System.InvalidOperationException</ExceptionType> 
    <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.SendAsyncInternal(HttpRequestMessage request, CancellationToken cancellationToken) 
    at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)</StackTrace> 
</Error> 

助けてくださいどのように異なるタイプのパラメータで同じPostメソッドを持つことができますか

私はText (text/plain)

としてデータを送信しています

EDIT 1:

enter image description here

答えて

3

あなたは別のルートまたはウェブAPI用のアクション

にマップする必要が同じコントローラで複数のPOSTメソッドを使用する場合は1

WebApiConfigにルートを追加します。詳細はanswerですが、重要なことはデフォルトのルートapi/controller/idを指定することです整数だけを受け入れます。そうでなければ、アクションは文字列IDとして扱われます。

routes.MapHttpRoute(
    name: "ControllerAndId", 
    routeTemplate: "api/{controller}/{id}", 
    defaults: null, 
    constraints: new { id = @"^\d+$" } // Only integers 
); 
routes.MapHttpRoute(
    name: "ActionApi", 
    routeTemplate: "api/{controller}/{action}" 
); 

はその後、コントローラでここ2

あなたは

[RoutePrefix("api/data")] 
public class DataController : ApiController 
{ 
    [HttpPost] 
    [Route("post1")]  
    public HttpResponseMessage Post([StringBody]string data) 
    { 
     // Logic 
    } 

    [HttpPost] 
    [Route("post2")] 
    public HttpResponseMessage Post(Requirements objRequirement) 
    { 
     //Logic 
    } 
} 

最初にルーティング属性を使用することができますのWeb APIの方法

public class DataController : ApiController 
{ 
    [HttpPost] 
    [ActionName("post1")]  
    public HttpResponseMessage Post([StringBody]string data) 
    { 
     // Logic 
    } 

    [HttpPost] 
    [ActionName("post2")] 
    public HttpResponseMessage Post(Requirements objRequirement) 
    { 
     //Logic 
    } 
} 

の岩下アクションを指定しますpostメソッドはapi/data/post1とt彼は2番目のAPI /データ/ポスト2

+0

をサポートしていません新しいクラスを追加することを教えてくださいedit1 –

+0

[Route( "")]の同じクラス用の生成クラス –

+0

申し訳ありません、使用していませんでしたweb api 1. Web api 2では、私の解決策が基づいている属性ルーティングを使用することができます –

関連する問題