ASP.NET Core - Redirect all requests to a single URL or Route How to redirect all non-static HTTP Requests to a single Page, View, or static file in your ASP.NET Core MVC app

How to fix the "No executable found matching command dotnet-ef" error in Visual Studio with .NET Core

When you are developing an ASP.NET Core application, a typical need you might have is to create a "catch-all" page to show to all your users instead of the web page they have requested, thus preventing them from actively using the website.

Such a requirement usually happens during certain scenarios, such as:

  • Work In Progress. When the website isn't yet ready, but you still want your users to be notified of something that will eventually be available.
  • Maintenance. When you are forced to put your website down for a certain time to perform some maintenance tasks.
  • Temporary Unavailable. When you want to shut down your website.

And so on.

It goes without saying that, since you still want to use your website UI/UX to design the catch-all page, you don't want to redirect all the requests, as your users will still need to get the required JS, CSS, and/or font files to properly render the page. In other words, you need to redirect only the requests pointing to non-static files. In this article, we'll see how to do that.

Adding the Action Method

The first thing we need to do is to create the Action Method that will show the catch-all page. To do that we can add a new Controller or use an existing one (such as the HomeController). In the following example, we can see a sample Action Method called WorkInProgress, which - when executed - doesn't do anything more than show a simple Razor View.

For the sake of simplicity, we'll take for granted that we added the above Action Method to our existing HomeController - thus meaning that it can be accessed using the /Home/WorkInProgress relative URL.

Now that we have defined the action method (and its URL endpoint), we need to set up its corresponding View.

Adding the View

Here's a sample View that we can use:

That's it: needless to say, we can also take the chance to suppress the Layout (adding a Layout = null source code line in the first razor code block) if we don't want to use the website's default layout View, or do anything else we want. Once done, we can proceed to the next step.

WARNING: If you don't want to use the Layout view, consider creating a static HTML file for your catch-all page instead of using the Action Method + View approach.

Adding the Middleware

All that is missing now is the logic that will redirect all HTTP requests not pointing to static files to the route corresponding to the new page/view. We can do that by adding the following code in the Program.cs file, right below the Static Files Middleware:

It's important to put the above controller after the Static Files Middleware, because the middlewares are executed in order: by doing that, we will only affect the requests not previously handled by the Static Files Middleware - which is precisely what we want.

Conclusion

That's it for now: we hope that this small guide will be useful for the ASP.NET developers looking for a simple, yet effective way to create a catch-all page to redirect all requests to.

 

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.