0

So I have this problem that I have endpoint like

        [HttpGet("get/{id}")]
        public async Task<IActionResult> Get([FromRoute] long? id)
        {
            if (id == null)
                return BadRequest();
            var result = await Mediator.Send(new GetIssueByIdQuery(id));
            return CreateResponse(result);
        }

and if I send request like

..../get/1

everything works fine. But if I give id parameter of different type, eg.:

.../get/asd

the server automagically responds with some generic validation error and 404. Since the request doesn't even hit the endpoint, how can I handle this situation myself and return some more descriptive information to the client?

1

1 Answer 1

2

You could change type of id parameter to the string and try to parse it by yourself:

public async Task<IActionResult> Get([FromRoute] string id)
{
    if (string.IsNullOrWhiteSpace(id))
        return BadRequest($"{nameof(id)} parameter should not be empty");
    if (!long.TryParse(id, out var longValue))
        return BadRequest($"{nameof(id)} should be convertible to long");

    var result = await Mediator.Send(new GetIssueByIdQuery(longValue));
    return CreateResponse(result);
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.