Different Types Of Action Results In ASP.NET MVC

Here in this article, we will learn about the different type of action results available in ASP.NET MVC 5. There are various action methods in MVC 5 but ActionResult is the base class for all the action methods available in MVC 5.

There are some restrictions also:

  • It cannot be overloaded
  • It cannot be a static method
  • It must be public, nor private or protected are allowed.

different-types-of-action-results-in-asp-net-mvc

different-types-of-action-results-in-asp-net-mvc

As we can see it is the base class which is defined as abstract class.

Types of Action Results
  • View Result
  • Redirect Result
  • Redirect To Route Result
  • File Result
  • Partial View Result
  • Redirect To Action Result
  • Json Result
  • Content Result

View Result

It returns the view page which has .cshtml extension in controller folder.

public ViewResult ViewDemo()  
       {   
            return View();  
       }

Partial View Result

It returns the partial view as a result. Partial view is placed inside a Normal view page.

public PartialViewResult Sidebar()  
{  
       return PartialView("_Sidebar");  
}

Redirect Result

It returns the result to a specific URL. It renders to the page by URL

public RedirectResult About()  
{  
       return Redirect("Contact");  
}

Redirect to Action Result

It returns the result to the specific controller and action method.

public ActionResult Index()  
{  
        return RedirectToAction("About", "Manage");  
}

Json Result

It is useful when we call the method using the ajax.

public JsonResult Index()  
{   
       return Json("Successful", JsonRequestBehavior.AllowGet);  
}

File Result

It returns different file format view page using file result

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

Content Result

It returns different content’s format to view.

public ActionResult Index()  
{  
      return Content("<script>alert('Content Result');</script>");  
}

Submit a Comment

Your email address will not be published.

Subscribe

Select Categories