0
votes

I am trying ASP.NET Core 3.1, MVC.

I am confused with the return type.

According to many links, such as https://docs.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)

1
There's various documentation about controller method return types here: docs.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. - Martin Costello
@MartinCostello, I'm not sure why you posted as a comment. This is a perfect answer. - MyDaftQuestions

1 Answers

2
votes

This choice would be a good choice for mistakes

public IActionResult Get()
 if(username == password )
 {
     return Ok(username);
 }else
 {
    return BadRequest("False");
 }