DEV Community

Sachin Ghatage
Sachin Ghatage

Posted on

What’s the difference between IActionResult and ActionResult<T> in ASP.NET Core Web API?

IActionResult is an interface that represents a general action result. You can return any kind of HTTP response, such as Ok(), NotFound(), BadRequest(), etc. It doesn’t specify the type of data being returned.

public IActionResult GetUser()
{
var user = _userService.GetUser();
if (user == null)
return NotFound();
return Ok(user);
}

ActionResult is a generic type that allows you to return either a typed object (T) or an HTTP response. It combines the flexibility of IActionResult with the strong typing of T.

public ActionResult GetUser()
{
var user = _userService.GetUser();
if (user == null)
return NotFound();
return user; // Directly return T (User)
}

Top comments (0)