ASP.NET Core map route to static file handler

This seems to work:

app.MapWhen(
    context => {
        var path = context.Request.Path.Value.ToLower();
        return
            path.StartsWith("/assets") ||
            path.StartsWith("/lib") ||
            path.StartsWith("/app");
    },
    config => config.UseStaticFiles());

However, I'm not sure if there are any performance (or other type of) implications. I'll update if I come across any.


It is strange that this common case (since many use SPA) is not covered almost anywhere and everyone has to invent something. I have found that the best way to do that is to add constraint (e.g. do not use the route if there is /api or "." in the path). Unfortunately this is not supported out of the box, but you can write this constraint yourself or copy the one I wrote from here.

There are a bit more details in this post. But generally the code looks like this:

 app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "api",
            template: "api/{controller}/{action}");

        routes.MapRoute(
            name: "angular",
            template: "{*url}",
            defaults: new {controller = "Home", action = "Index"},
            constraints: new {url = new DoesNotContainConstraint(".", "api/") });                
    });

P.S. Perhaps this constraint exist out of the box now, but I have not found one. Alternatively a RegEx can be used, but simple one should be way faster.