2017-04-15 9 views
0

私のコントローラには以下のようなAPIアクションがあります。Web API Route Configを設定しています

[RoutePrefix("api/export")] 
    public class ExportController : ApiController 
    { 
     [HttpPost] 
     public HttpResponseMessage Report([FromBody]ReportInput input, string reportType) 
     { 
     } 
    } 

そして、このようなルート設定に設定を追加しました。

 config.Routes.MapHttpRoute(
      name: "DefaultApi", 
      routeTemplate: "api/{controller}/{action}/{reportType}", 
      defaults : new {reportType = RouteParameter.Optional} 
     ); 

私は以下のAPI URLを呼び出すことはできません。どの設定を行う必要がありますか?

localhost:50773/api/export/report/InsuranceHandlingFiles

答えて

0

あなたは([RoutePrefix]の形で)すでにatttribute routingを使用しているように見えます。あなたは完全にそれに切り替えることができます。

config.MapHttpAttributeRoutes(); 

をそして、あなたのコントローラメソッドに追加[Route]属性を追加し、/api/export/report/InsuranceHandlingFilesのようなURLをマッピングするために:代わりに、あなたの現在のルート設定のため、あなたは単にこれを行うだろう

[RoutePrefix("api/export")] 
public class ExportController : ApiController 
{ 
    [HttpPost, Route("report/{reportType}")] 
    //   ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
    //     add this 
    public HttpResponseMessage Report([FromBody]ReportInput input, string reportType) 
    { 
     … 
    } 
} 

あなたの場合reportTypeをオプションにして、string reportTypeパラメータにデフォルト値を割り当て、それだけでは不十分な場合は、2番目のルートを追加します。例:

[HttpPost, Route("report/{reportType}"), Route("report")] 
public HttpResponseMessage Report([FromBody]ReportInput input, string reportType = null) 
{ 
    … 
} 
関連する問題