Introduction
This article overviews redirect action results in ASP.NET Core MVC. We will lean all redirect action results step by step with examples. Please read the previous article about ActionResult in ASP.net Core MVC. The code is here.
There are four types of redirect action results in ASP.Net Core MVC. Each one can either return normal redirect or permanent. The return method related to the permanent one is suffixed with the Permanent keyword. You can also return these results with their Permanent property set to true. These action results are:
- RedirectResult
- RedirectToActionResult
- RedirectToRouteResult
- LocalRedirectResult
RedirectResult
RedirectResult is an ActionResult that returns a Found (302), Moved Permanently (301), Temporary Redirect (307), or Permanent Redirect (308) response with a Location header to the supplied URL. It will redirect us to the provided URL, it doesn’t matter if the URL is relative or absolute.
- public RedirectResult MyProfile()
- {
- return Redirect("https://www.c-sharpcorner.com/members/farhan-ahmed24");
- }
- public RedirectResult Profile()
- {
- return RedirectPermanent("https://www.c-sharpcorner.com/members/farhan-ahmed24");
- }
If we call the RedirectPermanent method, it redirect us permanently. Also as I explained in previous section we don’t need to use these methods to redirect permanently or temporarily, we can just new up an instance of RedirectResult with its Permanent property set to true or false and return that instead.
- return new RedirectResult("/") { Permanent = true};
RedirectToActionResult
RedirectToActionResult is an ActionResult that returns a Found (302), Moved Permanently (301), Temporary Redirect (307), or Permanent Redirect (308) response with a Location header. It targets a controller action, taking in action name, controller name, and route value.
- public RedirectToActionResult EmployeeList()
- {
- return RedirectToAction("Index", "Employees");
- }
RedirectToRouteResult
RedirectToRouteResult is an ActionResult that returns a Found (302), Moved Permanently (301), Temporary Redirect (307), or Permanent Redirect (308) response with a Location header. Targets a registered route. It should be used when we want to redirect to a route. It takes a route name, route value and redirect us to that route with the route values provided.
- public RedirectToRouteResult DepartmentList()
- {
- return RedirectToRoute(new { action = "Index", controller = "Departments", area="" });
- }
LocalRedirectResult
LocalRedirectResult is an ActionResult that returns a Found (302), Moved Permanently (301), Temporary Redirect (307), or Permanent Redirect (308) response with a Location header to the supplied local URL. We should use LocalRedirectResult if we want to make sure that the redirects occur in some context that are local to our site. This action result type takes a string for URL needed for redirect, and a bool flag to tell it if it’s permanent. Under the hood, it checks the URL with the
Url.IsLocalUrl("URL") method to see if it’s local. If it redirects us to the address, but if it wasn’t, it will throw an InvalidOperationException. You cannot pass a local URL with an absolute address like this, http://localhost:52513/Home/Index, you’ll get an exception. That’s because the IsLocalUrl method considers a URL like this to not be local, so you must always pass a relative URL in.- public LocalRedirectResult LocalRedirect()
- {
- return LocalRedirect("/Home/Index");
- }


Rod FalangaPosted Feb 10, 2022, 9:41 PM
Thank you, Farhan. Do you have a similar blog post for older .NET frameworks? I'm using .NET Framework 4.6.2