gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:12:01 30 4
gpt4 key购买 nike

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

目前我有...

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

在我的 startup.cs 文件中,但它不会使用在我的 dist 文件夹中生成的 index.html。关于我需要改变什么的任何想法?这些文档没有从我可以找到的关于如何执行此操作的内容中提供太多引用。

谢谢。

最佳答案

您可以使用如下代码,但只有在 的根目录中放置空 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。

更新 :使用 有一个简单而漂亮的解决方案来处理这个问题。 WebRoot Program.cs像下面这样:
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 Startup.cs的方法你只需使用:
    app.UseDefaultFiles();
app.UseStaticFiles();

关于asp.net-core - .NET Core 更改我的默认 index.html 文件所在的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45316230/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com