我们如何设置ContentRootPath和WebRootPath?

时间:2016-04-14 22:19:22

标签: asp.net-core

当我们从IIS运行我们的应用时,我们最终会使用以下ContentRoot和WebRoot。

ContentRoot:  C:\MyApp\wwwroot
WebRoot:      C:\MyApp\wwwroot\wwwroot

以下是我们设置ContentRootWebRoot的方式。

public class Startup
{
    private readonly IHostingEnvironment _hostingEnv;

    public Startup(IHostingEnvironment hostingEnv)
    {
        _hostingEnv = hostingEnv;
    }

    public void Configure(IApplicationBuilder app)
    {
        app.Run(context =>
        {
            // test output
            context.Response.WriteAsync(_hostingEnv.ContentRootPath + "\r\n");
            return context.Response.WriteAsync(_hostingEnv.WebRootPath + "\r\n");
        });
    }

    public static void Main(string[] args)
    {
        var contentRoot = Directory.GetCurrentDirectory();
        var webRoot = Path.Combine(contentRoot, "wwwroot");

        var host = new WebHostBuilder()
            .UseKestrel()
            .UseIISPlatformHandlerUrl()
            .UseContentRoot(contentRoot)  // set content root
            .UseWebRoot(webRoot)          // set web root
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}

从intellisense我看到......

  • ContentRootPath 包含应用程序内容文件。
  • WebRootPath 包含可通过网络处理的内容文件。

我们如何使测试输出看起来像这样:

ContentRoot:  C:\MyApp\
WebRoot:      C:\MyApp\wwwroot\

2 个答案:

答案 0 :(得分:3)

虽然RC2文档仍在准备中,但我在尝试将pre-RC2应用程序部署为Azure Web App时学到了以下内容:

  1. 还没有Visual Studio工具,因此必须通过FTP手动发布和部署应用程序。要进行发布,请使用:dotnet publish --configuration Release --output ./approot

  2. 如果通过FTP连接到Azure,您可能会看到类似于:

  3. 的内容

    enter image description here

    1. “approot”文件夹可以替换为已发布的文件夹(web.config保留在审批中)。

    2. 必须将“approot”配置为Azure门户中的虚拟应用程序(默认为site \ wwwroot):

    3. enter image description here

      1. 从wwwroot文件夹获取静态文件的最后一件事,应修改Startup.cs文件以包含自定义UseWebRoot调用:
      2. var currentDirectory = Directory.GetCurrentDirectory();
        
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseWebRoot(Path.Combine(currentDirectory, "..", "wwwroot"))
            .UseDefaultHostingConfiguration(args)
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();
        

        完成这些步骤后,您应该在Azure上运行ASPNET Core pre-RC2 Web应用程序。

答案 1 :(得分:0)

在RC2中,如果我们将web.config放在wwwroot旁边,并将IIS指向MyApp目录,就像这样......

MyApp
  web.config
  wwwroot

...来自原始问题的代码输出此...

ContentRoot:  C:\MyApp\
WebRoot:      C:\MyApp\wwwroot\