c# – Web Api可选参数位于中间,带有属性路由

c# – Web Api可选参数位于中间,带有属性路由,第1张

概述所以我正在用Postman测试我的一些路由,我似乎无法接受这个调用: API函数 [RoutePrefix("api/Employees")]public class CallsController : ApiController{ [HttpGet] [Route("{id:int?}/Calls/{callId:int?}")] public async Task< 所以我正在用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();    }}
总结

以上是内存溢出为你收集整理的c# – Web Api可选参数位于中间,带有属性路由全部内容,希望文章能够帮你解决c# – Web Api可选参数位于中间,带有属性路由所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1247404.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存