.net Core - StaticFiles with User DefaultFiles

I have the following project hierarchy:

-MyDotNetProject ... L wwwroot L angular L src L angulardev.html L index.html 

and now I want to use angulardev.html as a starting point when starting the application.

I tried to execute in Startup.cs / Configure, but it does not work.

 DefaultFilesOptions defaultFilesOptions = new DefaultFilesOptions(); defaultFilesOptions.DefaultFileNames.Clear(); defaultFilesOptions.DefaultFileNames.Add("angulardev.html"); app.UseDefaultFiles(defaultFilesOptions); var path = Path.Combine(env.ContentRootPath, "angular/src"); var provider = new PhysicalFileProvider(path); var options = new StaticFileOptions(); options.RequestPath = ""; options.FileProvider = provider; app.UseStaticFiles(options); 
+5
source share
3 answers

I use this:

  app.Use(async (context, next) => { await next(); if (context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value) && !context.Request.Path.Value.StartsWith("/api/")) { context.Request.Path = "/angulardev.html"; await next(); } }); app.UseAuthentication(); app.UseDefaultFiles(); app.UseStaticFiles(); app.UseMvcWithDefaultRoute(); 
+4
source

Now it works with me from a mix from @Irena Rich and my previous solution:

 if (env.IsDevelopment()) { app.Use(async (context, next) => { await next(); if (context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value) && !context.Request.Path.Value.StartsWith("/api/")) { context.Request.Path = "/src/angulardev.html"; await next(); } }); DefaultFilesOptions options = new DefaultFilesOptions(); options.DefaultFileNames.Clear(); options.DefaultFileNames.Add("angulardev.html"); app.UseDefaultFiles(options); app.UseStaticFiles(); app.UseStaticFiles(new StaticFileOptions() { FileProvider = new PhysicalFileProvider( Path.Combine(Directory.GetCurrentDirectory(), @"angular")), RequestPath = "" }); } 
+1
source

When you change the download location of static files, it does not change where the default files are downloaded from.

If you look at the actual middleware code on Github: https://github.com/damianh/StaticFilesMiddleware/blob/475aef7f56b92d6420ec700dcb397841b6842ed8/src/Microsoft.Owin.StaticFiles/DefaultFilesMiddleware.cs

You can see that it is still trying to download the file manually. It does not care what parameters you set for StaticFiles. Mostly because I assume that you might have a default page without using the middleware static files.

Remember that when calling app.UseDefaultFiles () it is middleware, not customization. It actually β€œdoes” something to try to return this file to you.

In any case, if you want to use a non-standard folder to load the default page, you just need to go to the same provider. For example, this code should work well for you:

 DefaultFilesOptions defaultFilesOptions = new DefaultFilesOptions(); defaultFilesOptions.DefaultFileNames.Clear(); defaultFilesOptions.DefaultFileNames.Add("angulardev.html"); defaultFilesOptions.FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"angular", @"src")); defaultFilesOptions.RequestPath = new PathString(""); app.UseDefaultFiles(defaultFilesOptions); app.UseStaticFiles(new StaticFileOptions() { FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"angular", @"src")), RequestPath = new PathString("") }); 
0
source

Source: https://habr.com/ru/post/1271851/


All Articles