Web Api 中间带属性路由的可选参数
Web Api Optional Parameters in the middle with attribute routing
所以我正在用 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 都会导致问题。
其实你不需要在路由中指定可选参数
[Route("Calls")]
或者您需要更改路线
[Route("Calls/{id:int?}/{callId:int?}")]
public async Task<ApiResponse<object>> GetCall(int? id = null, int? callId = null)
我会将路线更改为:
[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
可选参数必须在路由模板的末尾。所以你想做的事是不可能的。
Attribute routing: Optional URI Parameters and Default Values
您要么更改路线模板
[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();
}
}
所以我正在用 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 都会导致问题。
其实你不需要在路由中指定可选参数
[Route("Calls")]
或者您需要更改路线
[Route("Calls/{id:int?}/{callId:int?}")]
public async Task<ApiResponse<object>> GetCall(int? id = null, int? callId = null)
我会将路线更改为:
[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
可选参数必须在路由模板的末尾。所以你想做的事是不可能的。
Attribute routing: Optional URI Parameters and Default Values
您要么更改路线模板
[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();
}
}