I've been searching for this question for a while but have yet to find an answer. In a nutshell, I have a .net core web application using the angular spa template. I have multiple controllers that act as APIs and in general everything works fine. However in the following situation, I'm curious if there is another way to accomplish a null id being sent WITHOUT overloading the action.
Here is what I have and works as expected:
[HttpGet("[action]")]
public IActionResult SayHello()
{
return BadRequest(new
{
error = "Name registered as null.",
message = "No value(null) was provided to the api resource 'SayHello' which resulted in no action taken."
});
}
[HttpGet("[action]/{name}")]
public IActionResult SayHello(string name)
{
return Ok($"Hello {name}");
}
What I want to know is this, can I accomplish this WITHOUT overloading? For example, simply do this:
[HttpGet("[action]/{name}")]
public IActionResult SayHello(string name)
{
if (name == null)
{
return BadRequest(new
{
error = "Name registered as null.",
message = "No value(null) was provided to the api resource 'SayHello' which resulted in no action taken."
});
}
return Ok($"Hello {name}");
}
Currently when I attempt this, the null id route will be treated as if it doesn't exist and the route will be passed to angular to deal with. Is overloading the preferred and only way to accomplish this? Or is there another way?