2017-03-27 11 views
2

に一致する私は、私は二つのコントローラを使用したWeb APIプロジェクトを使用しています:属性ルートが要求されたURL

、第1のコントローラは、以下のようなものです:

public class SmartlingController : BaseApiController 
{ 
    [Route("api/smartling/ProcessSmartlingTranslation")] 
    [VersionedRoute("", 1)] 
    [ResponseType(typeof(HttpResponseMessage))] 
    [HttpPost] 
    public IHttpActionResult ProcessSmartlingTranslation(HttpRequestMessage request) 
    { 
     //some business logic 
    } 
} 

第二コントローラ:

public class CommentsController : BaseApiController 
{ 
    [Route("api/comments/GetAndPostBlogComments")] 
    [VersionedRoute("", 1)] 
    [ResponseType(typeof(HttpResponseMessage))] 
    [HttpPost] 
    public IHttpActionResult GetAndPostBlogComments([FromBody] BlogAndStoryComment comment) 
    { 
     //some business logic 
    } 
    [Route("api/comments/GetAndPostStoryComments")] 
    [VersionedRoute("", 1)] 
    [ResponseType(typeof(HttpResponseMessage))] 
    [HttpPost] 
    public IHttpActionResult GetAndPostStoryComments([FromBody] BlogAndStoryComment comment) 
    { 
     //some business logic 
    } 
} 

以下はwebapiの登録方法です:

public static void Register(HttpConfiguration config) 
{ 
    // Web API routes 
    config.MapHttpAttributeRoutes(); 
    config.Routes.MapHttpRoute(
     "DefaultApi", 
     "api/{controller}/{action}/{id}", 
     new { id = RouteParameter.Optional } 
    ); 
    var f = new FormUrlEncodedMediaTypeFormatter(); 
    f.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json")); 
    f.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/x-www-form-urlencoded")); 
    config.Formatters.Add(f); 
    var enableCorsAttribute = new EnableCorsAttribute("*", 
             "Origin, Content-Type, Accept", 
             "GET, PUT, POST, DELETE, OPTIONS"); 
    config.EnableCors(enableCorsAttribute); 
} 

私のコードはここで間違っていますが、どうすればこの問題を解決できますか?

+0

'[VersionedRoute(" "、1)]'これが問題です。それをテストするには、上記のアクションから属性を削除し、競合を解決する必要があります。 – Nkosi

答えて

1

この例では、バージョン化されたすべてのルートのテンプレートは同じです。これが競合するルートの理由です。バージョン管理されたルートテンプレートを更新して一意にするか、完全に削除してルートの競合を解決します。

public class SmartlingController : BaseApiController { 
    //POST api/smartling/ProcessSmartlingTranslation 
    [Route("api/smartling/ProcessSmartlingTranslation")] 
    [VersionedRoute("api/smartling/ProcessSmartlingTranslation", 1)] 
    [ResponseType(typeof(HttpResponseMessage))] 
    [HttpPost] 
    public IHttpActionResult ProcessSmartlingTranslation(HttpRequestMessage request) { 
     //some business logic 
    } 
} 

public class CommentsController : BaseApiController { 
    //POST api/comments/GetAndPostBlogComments 
    [Route("api/comments/GetAndPostBlogComments")] 
    [VersionedRoute("api/comments/GetAndPostBlogComments", 1)] 
    [ResponseType(typeof(HttpResponseMessage))] 
    [HttpPost] 
    public IHttpActionResult GetAndPostBlogComments([FromBody] BlogAndStoryComment comment) { 
     //some business logic 
    } 

    //POST api/comments/GetAndPostStoryComments 
    [Route("api/comments/GetAndPostStoryComments")] 
    [VersionedRoute("api/comments/GetAndPostStoryComments", 1)] 
    [ResponseType(typeof(HttpResponseMessage))] 
    [HttpPost] 
    public IHttpActionResult GetAndPostStoryComments([FromBody] BlogAndStoryComment comment) { 
     //some business logic 
    } 
} 
+0

はい、あなたは正しいです。Nkosi、私はversionedrouteを削除し、問題は解決しました。私はupvotingと解答としてこの答えをマークしています。 –

関連する問題