Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Api Optional Parameters in the middle with attribute routing

So I'm testing some of my routing out with Postman and I can't seem to get this call to go through:

API Function

[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();
    }
}

Postman Requests

http://localhost:61941/api/Employees/Calls DOES NOT WORK

Error:

{
  "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 WORKS

http://localhost:61941/api/Employees/1/Calls/1 WORKS

Any idea why I can't use an optional between my prefix and the custom route? I've tried combining them into one custom route and that doesn't change anything, any time I try to cut out the id it causes problems.

like image 821
tokyo0709 Avatar asked Aug 02 '16 19:08

tokyo0709


2 Answers

Optional parameters must be at the end of the route template. so what you are trying to do is not possible.

Attribute routing: Optional URI Parameters and Default Values

you either change your route template

[Route("Calls/{id:int?}/{callId:int?}")]

or create a new action

[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();
    }
}
like image 111
Nkosi Avatar answered Nov 09 '22 10:11

Nkosi


I would change the Route to:

[Route("Calls/{id:int?}/{callId:int?}")]

and add the [FromUri] attribute to your parameters:

([FromUri]int? id = null, [FromUri]int? callId = null)

My test function looks like this:

[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);
}

I can invoke it using:

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
like image 4
Martin Brandl Avatar answered Nov 09 '22 09:11

Martin Brandl