0

I am trying ASP.NET Core 3.1, MVC.

I am confused with the return type.

According to many links, such as https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-3.1&tabs=visual-studio we set our signature to include the return type

EG

public async Task<ActionResult<TodoItem>> GetTodoItem()
{
    return new ToDoItem();
}

Historically I'd return a status code and the return object, such as

public async Task<ActionResult> GetToDoItem()
{
    return Ok(new ToDoItem());
}

It's really unclear why we don't return the status code in this manner any more. It's possible I'm getting confused by WebApi and MVC controllers, but as I've read more into this, I believe there is no real difference (in that it's simply an exposed end point)

2
  • 3
    There's various documentation about controller method return types here: learn.microsoft.com/en-us/aspnet/core/web-api/… The reason your first example works is because there's an implicit conversion from T to ActionResult<T> and the default status code for an action result is 200. Commented Jul 21, 2020 at 12:00
  • @MartinCostello, I'm not sure why you posted as a comment. This is a perfect answer. Commented Jul 21, 2020 at 12:04

1 Answer 1

2

This choice would be a good choice for mistakes

public IActionResult Get()
 if(username == password )
 {
     return Ok(username);
 }else
 {
    return BadRequest("False");
 }
Sign up to request clarification or add additional context in comments.

1 Comment

I like this approach because it is more flexible. Although, strictly speaking, this is MVC and not WebAPI. but in practice no one really cares.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.