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);
                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.
We must include Microsoft. AspNetCore. StaticFiles middleware in the request pipeline.
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 ).
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.
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();
                        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("")
});
                        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 = ""
      });
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With