.NET Core changing the location where my default index.html file is

后端 未结 2 419
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-03 07:51

I am building an Angular 2 app and using .NET Core and Webpack to compile my static resources into a dist folder. I am using a plugin that builds my index.html file and put

相关标签:
2条回答
  • 2021-01-03 08:32

    You can use code like below, but it only works when you put empty index.html in the root of wwwroot folder:

            app.UseDefaultFiles();
            // Serve wwwroot/dist as a root 
            app.UseStaticFiles(new StaticFileOptions() 
            {
                FileProvider = new PhysicalFileProvider(
                    Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\dist"))
            });
    

    RequestPath is empty by default so root path will show you index.html or default.html from wwwroot\dist folder.

    UPDATE: There is easy and beautiful solution to handle this issue using WebRoot in Program.cs like below:

    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseWebRoot(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "dist"))
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .UseApplicationInsights()
            .Build();
    
        host.Run();
    }
    

    In Configure method of Startup.cs you just use:

        app.UseDefaultFiles();
        app.UseStaticFiles();
    
    0 讨论(0)
  • 2021-01-03 08:38

    You are able to change the directory for the static files, by passing StaticFileOptions to the UseStaticFiles method:
    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files

    0 讨论(0)
提交回复
热议问题