October 25, 2021 10:04 by
Peter
IActionResult is an interface and ActionResult is an implementation of that interface.
ActionResults is an abstract class and action results like ViewResult, PartialViewResult, JsonResult, etc., derive from ActionResult.
Let's say you want to create an action result not catered to 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. With IActionResult 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.
For example see below,
publicNoContentResultNoContentActionResult() {
returnNoContent();
}
publicIActionResult NoContentActionResult() {
returnNoContent();
}
OR
publicIActionResult JsonActionResult() {
returnNotFound();
}
publicJsonResult JsonActionResult() {
returnNotFound(); // This line will give Error
}
From the 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 And JsonResult.
European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.