1

I have the following controller method:

[HttpGet]
[Route("api/users/{lookup}/{id?}")]
public async Task<IHttpActionResult> GetUsers(string lookup, [FromUri]string[] id = null) { //Can a parameter than comes FromUri ever be null?
    var repo = _uProvider.GetRepository(lookup);
    if (repo == null) {
        return Content(HttpStatusCode.NotFound, String.Concat(lookup, " is not a valid lookup type."));
    }
    if (id == null || id.Length == 0) { //added id.Length check because id was never null
        var all = await repo.GetAll();
        return Ok(all);
    }
    var users = await repo.GetUsersByIds(id);
    if (users == null) {
        return NotFound();
    }
    return Ok(users);
}

Why is it that when I submit a Get request to api/users/domain, the string[] id is never null? I had to add the id.Length == 0 check to catch the use case where a client would like a full list of users within the domain.

4
  • 1
    I do not believe that it can be null. I think that it would default to an empty array, as in the JSON string sent would be as "[]". Commented Apr 19, 2016 at 17:17
  • @DavidPine Yeah, I'm inclined to believe you, at least that's certainly what appears to be happening. So do you think it would be safe to remove the null default from the method signature? Or should I keep it there to indicate the id portion is optional, or would you consider the Route tag sufficient? If you were handed this code to maintain what would be your preference? Commented Apr 19, 2016 at 17:41
  • 1
    I think leave it. It says what you mean, and you have a check in place for it. Commented Apr 19, 2016 at 18:11
  • @DavidPine Thanks man. Commented Apr 19, 2016 at 18:12

1 Answer 1

2

I do not believe that it can be null. I think that it would default to an empty array, as in the JSON string sent would be as "[]".

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.