This post by Stephen Walther talks about redirecting all requests in an MVC project by modifying the web.config system.webServer / rewrite section.
http://stephenwalther.com/archive/2015/01/16/asp-net-5-and-angularjs-part-3-adding-client-routing
However, it seems wrong to have to reintroduce a web.config xml file into an ASP.NET 5 project.
Is there another way to do this in ASP.NET 5? Maybe via the new config.json?
RedirectToAction(String, String, RouteValueDictionary) Redirects to the specified action using the action name, controller name, and route values.
To redirect the user to another action method from the controller action method, we can use RedirectToAction method.
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.
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" });
Found this very nice answer on SO: https://stackoverflow.com/a/34890926/990356
Context:
I have a SPA application and ASP.NET Core serves some REST APIs + 'wwwroot/index.html'.
I handle my routes inside the SPA application (client side) and it does not work when the user refresh the browser with a given route.
Example: refreshing the browser with URL http://localhost:5000/Account
gives a 404 error because there is no file 'wwwroot/Account/index.html' on the server although this is valid route inside the client side app.
Solution:
public void Configure(IApplicationBuilder app)
{
app.UseMvc();
app.UseStatusCodePagesWithReExecute("/");
app.UseDefaultFiles();
app.UseStaticFiles();
}
Note that the order is important.
=> it means if a route does not match a file on the server or anything known (a REST API for example), the server serves "/" ('wwwroot/index.html') while keeping the URL intact.
Tested with a React/React Router app and ASP.NET MVC Core 1.1
I am deploying an AngularJS app on Azure with vnext. Thanks to Matt I was able to make this snippet:
app.Run(context =>
{
if(context.Request.Path.Value != "/api") context.Response.Redirect("/");
return Task.FromResult<object>(null);
}
);
It routes everything to the Angular App except my REST api calls.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With