だから私はPostman
で私のルーティングのいくつかをテストしています、そして私はこの呼び出しを通過させることができないようです:
API関数
[RoutePrefix("api/Employees")]
public class CallsController : ApiController
{
[HttpGet]
[Route("{id:int?}/Calls/{callId:int?}")]
public async Task<ApiResponse<object>> GetCall(int? id = null, int? callId = null)
{
var testRetrieve = id;
var testRetrieve2 = callId;
throw new NotImplementedException();
}
}
郵便配達員のリクエスト
http:// localhost:61941/api/Employees/Calls 機能しません
エラー:
{
"Message": "No HTTP resource was found that matches the request URI 'http://localhost:61941/api/Employees/Calls'.",
"MessageDetail": "No action was found on the controller 'Employees' that matches the request."
}
http:// localhost:61941/api/Employees/1/Calls WORKS
http:// localhost:61941/api/Employees/1/Calls/1 WORKS
プレフィックスとカスタムルートの間にオプションを使用できない理由は何ですか?私はそれらを1つのカスタムルートに組み合わせてみましたが、何も変更されません。IDを切り出そうとすると、問題が発生します。
オプションパラメータは、ルートテンプレートの最後にある必要があります。だからあなたがしようとしていることは不可能です。
属性ルーティング:オプションのURIパラメータとデフォルト値
ルートを変更するか
[Route("Calls/{id:int?}/{callId:int?}")]
または新しいアクションを作成します
[RoutePrefix("api/Employees")]
public class CallsController : ApiController {
//GET api/Employees/1/Calls
//GET api/Employees/1/Calls/1
[HttpGet]
[Route("{id:int}/Calls/{callId:int?}")]
public async Task<ApiResponse<object>> GetCall(int id, int? callId = null) {
var testRetrieve = id;
var testRetrieve2 = callId;
throw new NotImplementedException();
}
//GET api/Employees/Calls
[HttpGet]
[Route("Calls")]
public async Task<ApiResponse<object>> GetAllCalls() {
throw new NotImplementedException();
}
}
ルートを次のように変更します。
[Route("Calls/{id:int?}/{callId:int?}")]
[FromUri]
属性をパラメーターに追加します。
([FromUri]int? id = null, [FromUri]int? callId = null)
私のテスト関数は次のようになります:
[HttpGet]
[Route("Calls/{id:int?}/{callId:int?}")]
public async Task<IHttpActionResult> GetCall([FromUri]int? id = null, [FromUri]int? callId = null)
{
var test = string.Format("id: {0} callid: {1}", id, callId);
return Ok(test);
}
私はそれを呼び出すことができます:
https://localhost/WebApplication1/api/Employees/Calls
https://localhost/WebApplication1/api/Employees/Calls?id=3
https://localhost/WebApplication1/api/Employees/Calls?callid=2
https://localhost/WebApplication1/api/Employees/Calls?id=3&callid=2
実際には、ルートにオプションのパラメーターを指定する必要はありません
[Route("Calls")]
またはルートを変更する必要があります
[Route("Calls/{id:int?}/{callId:int?}")]
public async Task<ApiResponse<object>> GetCall(int? id = null, int? callId = null)