In your Startup
's Configure
method, where app
is your IApplicationBuilder
:
app.Run(context =>
{
context.Response.Redirect("/");
return Task.FromResult<object>(null);
});
This will send all otherwise unhandled requests to the root of your application. Place this last, after any UseStaticFiles()
or other middleware registrations.
Note that this will not capture registrations above this; if you have other routes on your server (such as controller actions, etc.), they will not be captured. This should work well with the added benefit that you don't need to exclude patterns such as in the example.
Alternatively...
If you're doing this for a Single Page Application, you probably want to allow deep linking for your users. I use a simple attribute routing for that:
[Route("", Order = -1)]
[Route("{*pathInfo}", Order = 1000)]
public async Task<IActionResult> Index(string pathInfo = "", CancellationToken cancellationToken = default(CancellationToken))
{
return View("UiView");
}
This will map default requests (/
) with priority while mapping all other requests (allowing default ordering, etc. to take priority) also to your "UiView".
If you don't want to use attribute routing, use the method as above with the following route mapping:
// Before all your routes
routeBuilder.MapRoute(
"Root",
"",
defaults: new { controller = "Home", action = "Index" });
// Your routes here
// After all your routes
routeBuilder.MapRoute(
"DeepLink",
"{*pathInfo}",
defaults: new { controller = "Home", action = "Index" });