ASP.NET 5 HTML5 History ASP.NET 5 HTML5 History asp.net asp.net

ASP.NET 5 HTML5 History


You were on the right track, but rather than sending back a redirect, we just want to rewrite the path on the Request. The following code is working as of ASP.NET5 RC1.

app.UseIISPlatformHandler();// This stuff should be routed to angularvar angularRoutes = new[] {"/new", "/detail"};app.Use(async (context, next) =>{    // If the request matches one of those paths, change it.    // This needs to happen before UseDefaultFiles.    if (context.Request.Path.HasValue &&        null !=        angularRoutes.FirstOrDefault(        (ar) => context.Request.Path.Value.StartsWith(ar, StringComparison.OrdinalIgnoreCase)))    {        context.Request.Path = new PathString("/");    }    await next();});app.UseDefaultFiles();app.UseStaticFiles();app.UseMvc();

One issue here is that you have to specifically code your angular routes into the middleware (or put them in a config file, etc).

Initially, I tried to create a pipeline where after UseDefaultFiles() and UseStaticFiles() had been called, it would check the path, and if the path was not /api, rewrite it and send it back (since anything other than /api should have been handled already). However, I could never get that to work.


I'm using:

app.UseMvc(routes =>{    routes.MapRoute(        name:"Everything",         template:"{*UrlInfo}",         defaults:new {controller = "Home", action = "Index"});}

This will cause all routes to hit the Home Index page. When I need routes to fall outside this, I add them before this route as this becomes a catch all route.


Why not use routing feature in MVC? In the Configure method in Startup.cs, you could modify the following:

        // inside Configure method in Startup.cs        app.UseMvc(routes =>        {            routes.MapRoute(                name: "default",                template: "{controller}/{action}/{id?}",                defaults: new { controller = "Home", action = "Index" });            // Uncomment the following line to add a route for porting Web API 2 controllers.            // routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}");        });