find and solve || findandsolve.com
Please wait.....

IActionResult Vs ActionResult

In my blog, I will try to set the difference between IActionResult and ActionResult and also the advantages of using the IActionResult.IActionResult vs ActionResult.IActionResult is an interface and ActionResult is an implementation of that interface in ASP.NET C#. There are no more differences between IActionResult and ActionResult from a usability perspective, but since IActionResult is the intended contract for action results, it’s better to use it as opposed to ActionResult.

IActionResult

IActionResult return type is appropriate which is return multiple ActionResult returns types are possible in an action and ActionResult types represent various HTTP status codes. Any non-abstract class deriving from ActionResult qualifies as a valid return type. Some of the common return types like as

BadRequestResult(400),
NotFoundResult(404),
OkObejctRestult(200).

For Example

[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(Product))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet("{myId}")]
public IActionResult GetById(int myId) {     if (!_repository.TryGetProductMyProduct(id, out var myProduct))     {         return NotFound();     }     return Ok(myProduct);
}

404:  status code is returned when the myProduct represented by myId doesn't exist in the underlying data store. The NotFound convenience method is invoked as shorthand for return a new NotFoundResult();.

200:   status code is returned with the MyProduct object when the product does exist. The Ok convenience method is invoked as shorthand for return a new OkObjectResult(product);.

ActionResult 

You would need to make a custom class that inherits because the ActionResult is an abstract class. There are some different types of action results in ASP.NET MVC which are given below.

  1. View Result
  2. Partial View Result
  3. Redirect Result
  4. Redirect To Action Result
  5. Redirect To Route Result
  6. Json Result
  7. File Result
  8. Content Result

View Result

    public ViewResult AboutUs()  
       {
            ViewBag.Message = "This is my About us Pageg.";
            return View();
       }  

Partial View Result 

This is  returning the result to Partial view page.Partial pageg can call inside Normal view page.

public PartialViewResult Example()  
{
return PartialView("_MyPartialPage");
}  

Redirect Result

public RedirectResult Index()  
{
return Redirect("Home/AboutUs");

Redirect to Action Result

public ActionResult Index()  
{
return RedirectToAction("MyActionName", "MyControllerName");
}  

Json Result

public ActionResult Index()  
{
var employee= new List<EmployeeInfo>
   {
    new EmployeeInfo{EmployeeId=1, FirstName="rahul", LastName="Sharma"},
     new EmployeeInfo{EmployeeId=2, FirstName="Devin", LastName="Kaslati"}
   };     return Json(employee, JsonRequestBehavior.AllowGet);
  }  

File Result

public ActionResult Index()  
{
return File("Web.Config", "text");

Content Result

public ActionResult Contact() 
{
ViewBag.Message = "My About us page.";
 return View();
}  


SingleOrDefault vs FirstOrDefault - C#

Static vs Dynamic Keywords In C# - C#

IHttpActionResult vs HttpResponseMessage - C#

String vs Stringbuilder - C#

ViewData VS ViewBag Vs TempData in C# 

Difference between IEnumerable and IQueryable - C#

Between IF Statement and Switch Statement 

Difference between ASP.NET Core and ASP.NET MVC

Mahira  khanna

Mahira khanna

I have the skills you need for you company blog, website, or other content materials

If findandsolve.com felt valuable to you, feel free to share it.

Comments



Report Response