1

I have a route like http://localhost:63037/api/futuresMarginRuns/7/data which is working however another controller API with route http://localhost:63037/api/futuresMarginRuns/2018-07-11/data is not working, even the breakpoint in the controller API is not hit. Here are the API signatures

    [HttpGet]
    [Route("/api/futuresMarginRuns/{id}/data")]
    public async Task<IActionResult> GetFuturesMarginRunDataAsync(long id)
    {
        var data = await _repository.GetAllAsync(id).ConfigureAwait(false);
        return Ok(data);
    }

    [HttpGet]
    [Route("/api/futuresMarginRuns/{runDate}/data")]
    public async Task<IActionResult> GetFuturesMarginRunDataByDateAsync(DateTime runDate)
    {
        var data = await _repository.GetAllAsync(runDate).ConfigureAwait(false);
        return Ok(data);
    }

In the first case I get json data but in the second one the breakpoint is not hit so looks like the route is not mapped to the API properly in which case I would expect an error, but i get empty []

How can I the API to work?

Thanks

1 Answer 1

2

You need to add some route constraints to your routes. Route constraints tell the routing engine that if id is supposed to be an int, only match that route if the text in that spot can be converted to an int (and similarly with dates, etc).

So I would change your routes to the following:

[HttpGet]
[Route("/api/futuresMarginRuns/{id:long}/data")]
public async Task<IActionResult> GetFuturesMarginRunDataAsync(long id)

[HttpGet]
[Route("/api/futuresMarginRuns/{runDate:datetime}/data")]
public async Task<IActionResult> GetFuturesMarginRunDataByDateAsync(DateTime runDate)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.