I have a simple API controller that accepts string values to identify an item to be deleted. So a DELETE call to /name/foo will delete foo, as expected.
When testing, I've found that URL encoding for a space %20 passed from the client as the first and only character of the final segment in a route (like /name/%20) causes the server to respond with a 500. When other characters are included, something like /name/first%20last, those values are properly passed through to the DeleteByName() method as first last, without issue.
When debugging, I've found that a DELETE to /name/%20 passes the framework validations as a valid route to be acted upon by the DeleteByName() method, but the name variable is null and throws a null pointer. I've resolved the null pointer by including a null check, but it doesn't feel like a best practice, and doesn't explain why this is happening in the first place.
Here's some example code
[Route("api/[controller]")]
[ApiController]
...
// DELETE: api/name
[HttpDelete("{name}")]
public async Task<IActionResult> DeleteByName(string name)
{
if (name == null) // this check handles the error, but doesn't explain why it happens
{
return NotFound();
}
await person.Delete(name); // without the null check, a null pointer is thrown on 'name'
return Ok();
}
Why is this null, as opposed to a string with one space ?
Is there a better way to handle this? (Like from the framework; without requiring a null check in the method body)
