Action Results In ASP.NET MVC Core
An Action Result in ASP.NET Core MVC is an Interface and IActionResult is the return type of controller method.
Each IActionResult returns a different type of response. If we want to return json object then we use JsonResult, Similar if we want to return View then we use View Result.
List of Action Results.
- ViewResult
- ContentResult
- JsonResult
- PartialViewResult
- RedirectResult
- RedirectToActionResult
- RedirectToPageResult
- RedirectToRouteResult
- StatusCodeResult
- ViewComponentResult
ViewResult
If you want to render View as response then use Viewresult.
using Microsoft.AspNetCore.Mvc;
namespace WebApplication4.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
}
}
ContentResult
If you to sent response as content like plain text, XML, html then you can use the content result.
When we use default content type then we are able to render string, if we want to display content in various formats then we just need to change the content type.
Rendering XML as output using the content result
If we want to render content as XML then we need to set content-type “application/xml”.
using Microsoft.AspNetCore.Mvc;
namespace WebApplication4.Controllers
{
public class HomeController : Controller
{
public ContentResult Index()
{
string data = @"<Contact>
<Name>Saineshwar</Name>
<Phone Type=""home"">206-555-0144</Phone>
<Phone type=""work"">425-555-0145</Phone>
<Address>
<Street1>123 Main St</Street1>
<City>Mercer Island</City>
<State>WA</State>
<Postal>68042</Postal>
</Address>
<NetWorth>10</NetWorth>
</Contact>";
return Content(data, "application/xml");
}
}
}

Rendering Text as output using content result.
If we want to render content as Plain then we need to set content-type ” text/plain”.
public ContentResult DisplayText()
{
string data = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,";
return Content(data, "text/plain");
}

Rendering Text as output using content result.
public ContentResult Default()
{
string data = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,";
return Content(data);
}

Rendering Html as output using the content result
If we want to render html as then we need to set content-type “text/html”.
public ContentResult DisplayHtml()
{
string data = @"<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
</body>
</html>";
return Content(data, "text/html");
}

JsonResult
If you want to render a Json object as a response then use JsonResult.
public JsonResult JsonResponse()
{
List<string> mystring = new List<string>();
mystring.Add("One");
mystring.Add("Two");
mystring.Add("Three");
mystring.Add("Four");
mystring.Add("Five");
return Json(mystring);
}
PartialViewResult
If you want to render PartialView as a response then use PartialViewResult.
For doing that I have added a partial view in a shared folder with name “_DemoPartial“.

public ActionResult PartialViewResponse()
{
List<string> ListofString = new List<string>();
ListofString.Add("One");
ListofString.Add("Two");
ListofString.Add("Three");
ListofString.Add("Four");
ListofString.Add("Five");
return PartialView("_DemoPartial", ListofString);
}

RedirectResult
If you want to redirect to specific URL then use Redirect Result.
When we used redirect result it returns 302 Found redirect status response code indicates that the resource requested has been temporarily moved to the URL given by the Location header.
Reference from:- https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/302
public ActionResult RedirectResultResponse()
{
return Redirect("https://www.bing.com");
}
RedirectToActionResult
If you want to redirect to specified action and controller then use RedirectToActionResult. If you specify action name and not controller name to RedirectToActionResult then it will search for that action inside the current controller where you are using RedirectToActionResult.
RedirectToActionResult with controller and action
public ActionResult RedirectToActionResponse1()
{
return RedirectToAction("Index", "Home");
}
RedirectToActionResult with only action
public ActionResult RedirectToActionResponse2()
{
return RedirectToAction("Index");
}
RedirectToPage
If you want to redirect to a specified pageName then use this RedirectToPageResult. It is mostly used in Razor pages.
Razor Pages is a new feature of ASP.NET Core MVC that makes coding page-focused scenarios easier and more productive. (definition from – https://docs.microsoft.com/en-us/aspnet/core/mvc/razor-pages/?tabs=visual-studio)
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace WebApplication4.Controllers
{
public class IndexModel : PageModel
{
public void OnGet()
{
}
public ActionResult OnPost()
{
return RedirectToPage("AllCustomer");
}
}
}
RedirectToRouteResult
If in an application we have more than one route and you want to redirect from one controller to another controller using route then you can use RedirectToRouteResult.
With Route
public ActionResult RedirectToRouteResponse()
{
return new RedirectToRouteResult(
new RouteValueDictionary(
new { action = "RedirectResultResponse",
controller = "Home"
}));
}
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
Without Route
public ActionResult RedirectToRouteResponse()
{
return new RedirectToRouteResult("default",
new RouteValueDictionary(
new { action = "RedirectResultResponse",controller = "Home"}));
}
StatusCodeResult
If you want to respond as an HTTP status code then you can use StatusCodeResult.
public StatusCodeResult StatusCodeResponse()
{
return StatusCode(200);
}
ViewComponentResult
If you want to send a response as ViewComponent then you can use ViewComponentResult.
What is ViewComponent?
View components are similar to partial views, but they’re much more powerful. View components don’t use model binding, and only depend on the data provided when calling into it
A view component:
- Renders a chunk rather than a whole response.
- Includes the same separation-of-concerns and testability benefits found between a controller and view.
- Can have parameters and business logic.
- Is typically invoked from a layout page.
Reference from:- https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-2.2
public class DisplayPrice : ViewComponent
{
public IViewComponentResult Invoke()
{
return View();
}
}
public IActionResult ViewComponentResponse()
{
return ViewComponent("DisplayPrice");
}