gpt4 book ai didi

asp.net-core - 我们如何设置ContentRootPath和WebRootPath?

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

当我们从 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();
}
}

从智能感知中我看到......

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

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

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

最佳答案

虽然 RC2 文档仍在准备中,但以下是我在尝试将 RC2 之前的应用程序部署为 Azure Web 应用程序时学到的知识:

  1. 尚无 Visual Studio 工具,因此必须通过 FTP 手动发布和部署应用程序。对于发布,请使用:dotnetpublish --configuration Release --output ./approot

  2. 如果通过 FTP 连接到 Azure,您可能会看到类似以下内容的内容:

enter image description here

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

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

  • enter image description here

  • 从 wwwroot 文件夹获取静态文件的最后一件事是,应修改 Startup.cs 文件以包含自定义 UseWebRoot 调用:
  • 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 应用程序。

    关于asp.net-core - 我们如何设置ContentRootPath和WebRootPath?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36635003/

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