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.
1 |
public IActionResult WorkInProgress() => 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:
1 2 3 4 5 6 7 8 |
@{ ViewData["Title"] = "Work in Progress"; } <div class="text-center"> <h1 class="display-4">Work in Progress</h1> <p>This website is not available.</p> </div> |
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.
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:
1 2 3 4 5 6 7 8 9 10 |
app.Use(async (context, next) => { var wipPath = "/Home/WorkInProgress"; if ((string)context.Request.Path != wipPath) { context.Response.Redirect(wipPath); return; } await next(); }); |
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.