Multiple optional parameters web api attribute routing

前端 未结 2 1533
余生分开走
余生分开走 2021-01-19 12:28

Hi guys i am new to attribute routing and not sure if this is even possible.

What i have is an attribute route which works fine like this

[HttpGet]
         


        
相关标签:
2条回答
  • 2021-01-19 12:47

    {flag:int=3?} is the problem. it is either optional {flag:int?} with the default value in the action or {flag:int=3}.

    [HttpGet]
    Route("GetIssuesByFlag/{flag:int=3}/{categoryId:int?}/{tagIds?}")]
    public IEnumerable<IssueDto> GetIssuesByFlag(int flag , int? categoryId = null, int?[] tagIds = null)
    

    You currently have 3 optional parameters. when you have just the 1 value routing table wont know which optional parameter you are referring to, hence the 404

    0 讨论(0)
  • 2021-01-19 12:56

    Use a query string.

    [HttpGet]
        [Route("GetIssuesByFlag/{flag:int=3?}")]
        public IEnumerable<IssueDto> GetIssuesByFlag(int flag, List<int> tagIds, int? categoryId = null)
    

    Url: /getissuesbyflag/1?tagIds=2,5,6&categoryId=56

    You really should use query strings for optional parameters and path parameters if they are required.

    0 讨论(0)
提交回复
热议问题