ASP.NET Master Map Route for Static File Handler

I am working on an ASP.NET Core website (formerly called ASP.NET 5 / vNext) with Angular. In order for Angular to work, I need to have the entire route:

app.UseStaticFiles(); 
app.UseMvc(routes =>
        {
            // Angular fallback route
            routes.MapRoute("angular", "{*url}", new { controller = "Home", action = "Index" });                
        });

I also have several files / folders in wwwroot, for example:

wwwroot/app
wwwroot/assets
wwwroot/lib

If any requests are made to these paths, for example http://example.com/assets/css/test.css, and the file ( test.css) DOES NOT exist, it should not continue the backup route. He must return 404.

Right now, if the file does not exist, it returns Angular HTML. So, how can I say that any path starting with '/ assets' should only be routed / served UseStaticFiles?

Thank.

+4
2

:

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

, - ( ). , -.

+4

, ( SPA) , - . , - (, , /api "." ). , , , .

. :

 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. , , . , RegEx , .

+1

All Articles