Take advantage of redirect action results in ASP.NET Core MVC to elegantly redirect a request to a specified URL Credit: Thinkstock 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. [ Also on InfoWorld: How to refactor God objects in C# ] 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, follow the steps outlined below to create a new ASP.NET Core project in Visual Studio. Launch the Visual Studio IDE. Click on “Create new project.” In the “Create new project” window, select “ASP.NET Core Web Application” from the list of templates displayed. Click Next. In the “Configure your new project” window, specify the name and location for the new project. Optionally check the “Place solution and project in the same directory” check box, depending on your preferences. Click Create. In the “Create a New ASP.NET Core Web Application” window shown next, select .NET Core as the runtime and ASP.NET Core 3.1 (or later) from the drop-down list at the top. 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 these 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"); } How to do more in ASP.NET Core: How to use attribute routing in ASP.NET Core How to pass parameters to action methods in ASP.NET Core MVC How to use API Analyzers in ASP.NET Core How to use route data tokens in ASP.NET Core How to use API versioning in ASP.NET Core How to use Data Transfer Objects in ASP.NET Core 3.1 How to handle 404 errors in ASP.NET Core MVC How to use dependency injection in action filters in ASP.NET Core 3.1 How to use the options pattern in ASP.NET Core How to use endpoint routing in ASP.NET Core 3.0 MVC How to export data to Excel in ASP.NET Core 3.0 How to use LoggerMessage in ASP.NET Core 3.0 How to send emails in ASP.NET Core How to log data to SQL Server in ASP.NET Core How to schedule jobs using Quartz.NET in ASP.NET Core How to return data from ASP.NET Core Web API How to format response data in ASP.NET Core How to consume an ASP.NET Core Web API using RestSharp How to perform async operations using Dapper How to use feature flags in ASP.NET Core How to use the FromServices attribute in ASP.NET Core How to work with cookies in ASP.NET Core How to work with static files in ASP.NET Core How to use URL Rewriting Middleware in ASP.NET Core How to implement rate limiting in ASP.NET Core How to use Azure Application Insights in ASP.NET Core Using advanced NLog features in ASP.NET Core How to handle errors in ASP.NET Web API How to implement global exception handling in ASP.NET Core MVC How to handle null values in ASP.NET Core MVC Advanced versioning in ASP.NET Core Web API How to work with worker services in ASP.NET Core How to use the Data Protection API in ASP.NET Core How to use conditional middleware in ASP.NET Core How to work with session state in ASP.NET Core How to write efficient controllers in ASP.NET Core Related content feature What is Rust? Safe, fast, and easy software development Unlike most programming languages, Rust doesn't make you choose between speed, safety, and ease of use. Find out how Rust delivers better code with fewer compromises, and a few downsides to consider before learning Rust. By Serdar Yegulalp Nov 20, 2024 11 mins Rust Programming Languages Software Development how-to Kotlin for Java developers: Classes and coroutines Kotlin was designed to bring more flexibility and flow to programming in the JVM. Here's an in-depth look at how Kotlin makes working with classes and objects easier and introduces coroutines to modernize concurrency. By Matthew Tyson Nov 20, 2024 9 mins Java Kotlin Programming Languages analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft unveils imaging APIs for Windows Copilot Runtime Generative AI-backed APIs will allow developers to build image super resolution, image segmentation, object erase, and OCR capabilities into Windows applications. By Paul Krill Nov 19, 2024 2 mins Generative AI APIs Development Libraries and Frameworks Resources Videos