Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net Core - StaticFiles with custom DefaultFiles-Definitions

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 the starting point when starting the application.

I tried following 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);
like image 648
Userino Avatar asked Sep 15 '17 17:09

Userino


People also ask

What is the default directory for static files in ASP.NET Core?

Static files are stored within the project's web root directory. The default directory is {content root}/wwwroot , but it can be changed with the UseWebRoot method.

Which of the following middleware must be installed to serve static files?

We must include Microsoft. AspNetCore. StaticFiles middleware in the request pipeline.

What does UseDefaultFiles do?

UseDefaultFiles is a URL rewriter that doesn't actually serve the file. Enable Static File Middleware via UseStaticFiles to serve the file. Based on this, it's important to first setup the URL rewriter ( UseDefaultFiles ) before serving the actual file ( UseStaticFiles ).

What is wwwroot folder in .NET Core?

By default, the wwwroot folder in the ASP.NET Core project is treated as a web root folder. Static files can be stored in any folder under the web root and accessed with a relative path to that root.


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();
like image 79
Irena Rich Avatar answered Oct 10 '22 09:10

Irena Rich


When you change where static files are being loaded from, it doesn't change where the "default" files are loaded 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 it's still trying to load the file manually. It doesn't care about what options you have set for StaticFiles. Mostly because I assume you can have a default page without using the static files middleware.

Remember, when you call app.UseDefaultFiles() it's a middleware, not a setting. It's actually "doing" something to try and return that file to you.

Anyway, if you want to use a non standard folder to load a default page, you just need to pass in the same provider. For example, this code should work fine 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("")
});
like image 30
MindingData Avatar answered Oct 10 '22 09:10

MindingData


It works now for me with a mix from @Irena Rich and my former 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 = ""
      });
}
like image 30
Userino Avatar answered Oct 10 '22 09:10

Userino