首页 文章

如何在 ASP.NET Core 中为 wwwroot 配置备用文件夹?

提问于
浏览
6

是否可以配置一个不同的文件夹来替换 ASP.NET Core 中的 wwwroot?如果是的话,这种变化有什么副作用?

目前在整个项目中包含 wwwroot 的唯一配置位于 project.json,如下面的代码所示;但是,使用新文件夹的名称替换该值不足以读取静态文件(例如:index.html)。

"publishOptions": {
"include": [
  "wwwroot",
  "web.config"
]
},

2 回答

  • 8

    是否可以配置一个不同的文件夹来替换 ASP.NET Core 中的 wwwroot?

    是。在Program类中添加UseWebRoot调用:

    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseWebRoot("myroot") // name it whatever you want
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();
    
        host.Run();
    }
    

    对这种变化有任何副作用吗?

    以下是我能想到的三个:

    • Bower 包管理器无法正常工作,因为它在wwwroot中查找lib文件夹。我不确定这是否可配置。

    • 您需要修复bundleconfig.json以查看新目录。

    • 您需要更新project.json中的include部分,以在发布输出中包含新目录。

  • 2

    使用 Asp.Net Core 2.2 我这样做了:在Setup.csConfigure方法中我改变了

    app.UseStaticFiles();
    

    app.UseStaticFiles(new StaticFileOptions
    {
      FileProvider = new PhysicalFileProvider(Path.Combine(
        AppDomain.CurrentDomain.BaseDirectory,
        "myStaticFolder")),
    });
    

    参考/来源和英文这里

相关问题