ASP.NET Core is a cross-platform, open source, lean, fast, and modular framework for building high-performance web applications. ASP.NET Core MVC applications enable you to redirect a request to a specified URL in several different ways. This article talks about how we can accomplish this with code examples wherever appropriate.
To work with the code examples provided in this article, you should have Visual Studio 2019 installed in your system. If you don’t already have a copy, you can download Visual Studio 2019 here.
Create an ASP.NET Core MVC project in Visual Studio
First off, let’s create an ASP.NET Core project in Visual Studio 2019. Assuming Visual Studio 2019 is installed in your system, please make sure you create ASP.NET Core Web Application through your Visual Studio.
- Select “Web Application (Model-View-Controller)” as the project template to create a new ASP.NET Core MVC application.
- Ensure that the check boxes “Enable Docker Support” and “Configure for HTTPS” are unchecked as we won’t be using those features here.
- Ensure that Authentication is set to “No Authentication” as we won’t be using authentication either.
- Click Create.
Following above steps will create a new ASP.NET Core MVC project in Visual Studio 2019. We’ll use this project in the sections below to illustrate how we can redirect requests when working with action methods in ASP.NET Core 3.1.
Redirect action results in ASP.NET Core MVC
There are several types of action results in ASP.NET Core MVC such as RedirectResult, RedirectToActionResult, RedirectToRouteResult, and LocalRedirectResult. All of these classes extend the ActionResult class and the IActionResult and IKeepTempDataResult interfaces and return Found (Http Status Code 302), Moved Permanently (Http Status Code 301), Temporary Redirect (Http Status Code 307), or Permanent Redirect (Http Status Code 308).
We’ll examine how we can work with each of these in this section.
Use RedirectResult in ASP.NET Core MVC
You can use any of the following methods to return a RedirectResult:
- Redirect – Http Status Code 302 Found (temporarily moved to the URL provided in the location header)
- RedirectPermanent – Http Status Code 301 Moved Permanently
- RedirectPermanentPreserveMethod – Http Status Code 308 Permanent Redirect
- RedirectPreserveMethod – Http Status Code 307 Temporary Redirect
The following lines of code show how you can use each of these methods.
Redirect("/Author/Index"); RedirectPermanent("/Author/Index"); RedirectPermanentPreserveMethod("/Author/Index"); RedirectPreserveMethod("/Author/Index");
Alternatively, you can return an instance of RedirectResult as shown in the code snippet given below.
public RedirectResult Index() { return new RedirectResult(url: "/Author/Index", permanent: true, preserveMethod: true); }
Note that the Redirect method can be used to redirect a request to a specified URL. This method is available in the abstract base class called ControllerBase.
public RedirectResult Index() { return Redirect("https://google.com"); }
It should be noted that the controllers you create in ASP.NET Core MVC extend the Controller class. This class in turn extends the ControllerBase class and implements the IActionFilter, IFilterMetadata, IAsyncActionFilter, and IDisposable interfaces.
Use RedirectToActionResult in ASP.NET Core MVC
This action result can be used to redirect to the specified action and controller. If no controller is specified it redirects to the specified action within the current controller. You can use any of the following methods to redirect to the specified action and return an instance of RedirectToActionResult from your action method.
- RedirectToAction – Http Status Code 302 Found (temporarily moved to the URL provided in the location header)
- RedirectToActionPermanent – Http Status Code 301 Moved Permanently
- RedirectToActionPermanentPreserveMethod – Http Status Code 308 Permanent Redirect
- RedirectToActionPreserveMethod – Http Status Code 307 Temporary Redirect
The following code snippet illustrates how the RedirectToAction method can be used.
public RedirectToActionResult Index() { return RedirectToAction(actionName: "Index", controllerName: "Author"); }
You can skip the controller name if you want to redirect the request to an action method in the current controller. The following code snippet shows how this can be achieved.
public RedirectToActionResult Index() { return RedirectToAction(actionName: "Privacy"); }
Use RedirectToRouteResult in ASP.NET Core MVC
This is yet another action result that can be used to redirect the request to the specified route. You can use any of the following methods to return an instance of RedirectToRouteResult from your action method.
- RedirectToRoute – Http Status Code 302 Found (temporarily moved to the URL provided in the location header)
- RedirectToRoutePermanent – Http Status Code 301 Moved Permanently
- RedirectToRoutePermanentPreserveMethod – Http Status Code 308 Permanent Redirect
- RedirectToRoutePreserveMethod – Http Status Code 307 Temporary Redirect
The following code snippet shows how the RedirectToRoute method can be used.
public RedirectToRouteResult Index() { return RedirectToRoute("author"); }
You can also specify the route value when redirecting as shown in the code snippet given below.
var routeValue = new RouteValueDictionary (new { action = "View", controller = "Author"}); return RedirectToRoute(routeValue);
Use LocalRedirectResult in ASP.NET Core MVC
This action result is used when you want to redirect to a local URL. It throws an InvalidOperationException if you use an external URL with it. You can use any of the following methods to return an instance of LocalRedirectResult from your action method.
- LocalRedirect – Http Status Code 302 Found (temporarily moved to the URL provided in the location header)
- LocalRedirectPermanent – Http Status Code 301 Moved Permanently
- LocalRedirectPermanentPreserveMethod – Http Status Code 308 Permanent Redirect
- LocalRedirectPreserveMethod – Http Status Code 307 Temporary Redirect
Redirect to razor pages in ASP.NET Core MVC
Finally, note that you can even redirect to razor pages using the RedirectToPage method, specifying the target razor page to redirect the request to. The RedirectToPage method returns a RedirectToPageResult instance together with a HTTP Status Code 302.
If you have a page named Author, where you want the request to be redirected, you can use the following code snippet.
public IActionResult RedirectToAuthorPage() { return RedirectToPage("Author"); }
Javier is Content Specialist and also .NET developer. He writes helpful guides and articles, assist with other marketing and .NET community work