加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 08:11:34 所属栏目:百科 来源:网络整理
导读:所以我正在用Postman测试我的一些路由,我似乎无法接受这个调用: API函数 [RoutePrefix("api/Employees")]public class CallsController : ApiController{ [HttpGet] [Route("{id:int?}/Calls/{callId:int?}")] public async TaskApiResponseobject GetCall(
所以我正在用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();
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读