【问题标题】:How to Configure an Alternate Folder to wwwroot in ASP.NET Core?如何在 ASP.NET Core 中将备用文件夹配置为 wwwroot?
【发布时间】:2016-11-10 19:19:52
【问题描述】:

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

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

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

【问题讨论】:

    标签: asp.net-core


    【解决方案1】:

    是否可以配置不同的文件夹来替换 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();
    }
    

    此更改是否有任何副作用?

    以下是我能想到的三个:

    1. Bower 包管理器将无法正常工作,因为它会在 wwwroot 中查找 lib 文件夹。我不确定这是否可配置。
    2. 您需要修复 bundleconfig.json 才能查看新目录。
    3. 您需要更新project.json 中的include 部分,以将您的新目录包含在发布输出中。

    【讨论】:

    • 关于#1,Bower 的路径可以在.bowerrc 文件中配置,该文件应该位于项目的根文件夹中。这是默认的:{ "directory": "wwwroot/lib" }
    【解决方案2】:

    使用 ASP.NET Core 2.2,在您的 StartupConfigure() 方法中,您可以更改以下内容:

    app.UseStaticFiles();
    

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

    Reference / Source (auch auf Deutsch)

    【讨论】:

      【解决方案3】:

      对于 ASP.NET Core 6,如果您使用新的 minimal hosting modelWebApplication class,那么最简单的方法是通过 WebRootPath propertyWebRootPath property 进行配置@:

      var builder = WebApplication.CreateBuilder(
          new WebApplicationOptions() 
          {
              WebRootPath = "mywebroot"
          }
      );
      

      在首先使用 Visual Studio 2022+ 或 .NET 6 SDK 模板生成的 Web 应用程序中默认配置最小托管模型,因此这可能是新的 ASP.NET Core 6 应用程序最熟悉的方法。

      虽然入口点已更改为WebApplication,但在技术上仍然可以按照the accepted answer 中的建议通过最小托管模型访问UseWebRoot(),但调用它会产生建议使用@ 的警告改为 987654332@。

      【讨论】:

        猜你喜欢
        • 2023-04-08
        • 1970-01-01
        • 2019-06-10
        • 2016-07-17
        • 2018-06-04
        • 2016-12-22
        • 2018-09-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多