Wednesday, November 14, 2018

IActionResult Vs ActionResult


IActionResult Vs ActionResult
IActionResult is an interface and ActionResult an implementation of that interface.
ActionResults is an abstract class and action results like ViewResult, PartialViewResult, JsonResult, etc derive from ActionResult.
When you will use IActionResult and create your own implementation of it
Let's say you want to create an action result not catered for by MVC, say an XML result.

IActionResult is an interface, we can create a custom response as a return, when you use ActionResult you can return only predefined ones for returning a View or a resource, withIActionResult  we can return a response, or error as well. On the other hand, ActionResult is an abstract class, and you would need to make a custom class that inherits and then further stuff.
For example as below
public NoContentResult NoContentActionResult()
{
        return NoContent();
}
public IActionResult NoContentActionResult()
{
        return NoContent();
}

OR

public IActionResult JsonActionResult()
{
      return NotFound();
}

public JsonResult JsonActionResult()
{
        return NotFound(); // This line will give Error
}

From above examples we can return NoContent() OR NotFound() from IActionResult But if you want to use such type of return values with ActionResults, you need to use particular ActionResults, NoContentResult AndJsonResult.

No comments:

Post a Comment

IActionResult Vs ActionResult

IActionResult  Vs   ActionResult IActionResult   is an interface and  ActionResult   an implementation of that interface. ActionRes...