1

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?

Drew
  • 23
  • 8

2 Answers2

1

Found the answer and it's rather simple. Just need to add another attribute to the action method.

[HttpGet("[action]")]
[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}");
}
Drew
  • 23
  • 8
0

Depending on what you do in the front end it is important to remember that the string can be empty rather than null. Additionally , you will then have to remove the route without the parameter. Consider the code below:

[HttpGet("[action]/{name}")]
public IActionResult SayHello(string name)
{
    if (string.IsNullOrEmpty(name))//this
    {
        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}");
}
Vince
  • 945
  • 7
  • 17
  • The problem is not null or empty unfortunately. The code simply never runs. MVC sees the lack of ID as a different route and thus looks for the different route. In my second example, the second route doesn't exist and thus it passes the route to the Angular router. This is the problem I'm trying to avoid. – Drew Jul 06 '20 at 00:42
  • @Drew hence, i mentioned that you have to remove the route without the parameter and have this one as the only one. – Vince Jul 06 '20 at 06:48
  • yeah, just re-read that but was thrown off by your code as you didn't remove the ID from the route. However, even if that is done, the problem continues. The route is simply passed on to angular to handle. I've found the answer and will reply with it. – Drew Jul 06 '20 at 20:11