Multiple optional parameters web api attribute routing
Asked Answered
B

2

6

I am new to attribute routing, and I am not sure if this is even possible.

I have an attribute route, which works fine like this:

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

Now I want to add some extra optional parameters to narrow down my search, so I want to add 2 extra optional parameters.

What I have tried:

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

This works fine if my call is /api/controller/1/2, but fails with 404 when it comes to /api/controller/1.

How can I achieve this?

Edit 1: Nkosi's answer below worked, however an extra modification was needed.

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

The list or array must be second as it is automatically null if no value is provided and cant be marked as optional with = null.

Bear answered 8/2, 2017 at 13:47 Comment(3)
{flag:int=3?} is the problem. it is either optional {flag:int?} with the default value in the action or {flag:int=3}. 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 404Moten
Nikosi's answer worked for me! Well the first one not the splitting one, having removed that everything works fine nowBear
first one(the remove optional from flag) can you update it on answers so i can mark as correct?Bear
M
4

{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

Moten answered 8/2, 2017 at 14:16 Comment(0)
M
1

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.

Meninges answered 8/2, 2017 at 14:6 Comment(1)
It works again if i provide all variables but if i provide just the flag it fails with "message": "No HTTP resource was found that matches the request URI 'localhost:44363/api/Issues/GetIssuesByFlag/1'.", "messageDetail": "No action was found on the controller 'Issues' that matches the request."Bear

© 2022 - 2024 — McMap. All rights reserved.