.NET Core更改我的默认index.html文件所在的位置

时间:2017-07-26 02:05:55

标签: asp.net-core

我正在构建一个Angular 2应用程序并使用.NET Core和Webpack将我的静态资源编译成dist文件夹。我正在使用一个插件来构建我的index.html文件并将其放在我的wwwroot / dist文件夹中,但我正在努力解决如何配置我的Startup.cs文件以查找dist文件夹中的默认文件。

目前我有......

        app.UseDefaultFiles();
        app.UseStaticFiles();

在我的startup.cs文件中,但它不会使用我的dist文件夹中生成的index.html。关于我需要改变什么的想法?对于如何做到这一点,文档没有提供太多参考。

感谢。

2 个答案:

答案 0 :(得分:7)

您可以使用下面的代码,但只有当您将空的index.html放在 wwwroot 文件夹的根目录中时,它才有效:

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

默认情况下,RequestPath为空,因此根路径将显示来自wwwroot \ dist文件夹的index.html或default.html。

更新:使用Program.cs中的 WebRoot 处理此问题是一种简单而美观的解决方案,如下所示:

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();
}

Configure的{​​{1}}方法,您只需使用:

Startup.cs

答案 1 :(得分:0)

您可以通过将StaticFileOptions传递给UseStaticFiles方法来更改静态文件的目录:
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files