Search code examples
c#owinsingle-page-applicationkatana

OWIN send static file for multiple routes


I'm making a SPA which sits on top of ASP.Net WebAPI. I'm waiting to use HTML5 history rather than #/ for history routing but that poses a problem for deep linking, I need to make sure / and /foo/bar all return the same HTML file (and my JS will render the right part of the SPA).

How do I get OWIN/Katana to return the same HTML file for multiple different urls?


Solution

  • To make things simple, while still keeping all the caching goodness etc. from the StaticFiles middleware, I'd just rewrite the request path using an inline middleware, like this

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.Map("/app", spa =>
            {
                spa.Use((context, next) =>
                {
                    context.Request.Path = new PathString("/index.html");
    
                    return next();
                });
    
                spa.UseStaticFiles();
            });
    
            app.UseWelcomePage();
        }
    }
    

    This will serve the welcome page on anything but /app/*, which will always serve index.html instead.