所以我正在用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工程
http://localhost:61941/api/Employees/1/Calls/1工作
知道为什么我不能在我的前缀和自定义路线之间使用可选项吗?我已经尝试将它们组合成一个自定义路线,这不会改变任何东西,任何时候我试图削减它导致问题的ID.
解决方法
可选参数必须位于路径模板的末尾.所以你要做的就是不可能.
Attribute routing: Optional URI Parameters and Default Values
你要么改变你的路线temaple
[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(); } }