c# - Azure 应用服务中的 IWebHostEnvironment ContentRootPath 与本地不同

标签 c# azure asp.net-core .net-core-3.0

我有一个 ASP.NET Core 3.0 项目。我正在从本地数据文件夹(在项目的根目录中)(在 wwwroot 文件夹之外!)读取一些数据。在本地运行时,我可以使用“IWebHostEnvironment.ContentRootPath”获取此路径。但是,将项目上传到 Azure 应用服务时,ContentRootPath 位于公共(public) wwwroot 文件夹中。当托管在 Azure 应用服务中时,如何访问数据文件夹?

最佳答案

根据我的测试,Azure Web应用程序中的内容根路径是D:\home\site\wwwroot,Azure Web应用程序中的webroot路径是D:\home\site\wwwroot\wwwroot。因此,您可能会误解项目中的 wwwroot 文件夹与 Azure Web App 上用于存储 Web 内容的 wwwroot 文件夹。

我的HomeController

 private readonly ILogger<HomeController> _logger;
        private readonly IWebHostEnvironment _env;
        public HomeController(ILogger<HomeController> logger, IWebHostEnvironment env)
        {
            _logger = logger;
            _env = env;
        }

        public IActionResult Index()
        {
            string contentRootPath = _env.ContentRootPath;
            string webRootPath = _env.WebRootPath;

            return Content("Content Root Path: "+contentRootPath + "\nWeb Root Path: " + webRootPath);
        }

我的网站网址:https://webv3.azurewebsites.net enter image description here

对此,我们也可以通过 Kudo 查看。 enter image description here enter image description here enter image description here

更多详情请引用ContentRootPath different in Development and Azure web app

关于c# - Azure 应用服务中的 IWebHostEnvironment ContentRootPath 与本地不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59308147/

相关文章:

c# - 多语言枚举

java - 如何使用 REST Api 在 IoT 中心注册新设备

c# - 对齐报告中的文本

c# - AutoMapper:字符串到可为空的 int

azure - 有没有办法使用 Azure CLI 或 Python SDK 访问计算配额?

asp.net-mvc - Azure 网站中的 ASP.NET MVC4 + Ninject + NLog = 问题

c# - 为什么 HttpPlatformHandler 不创建日志文件?

c# - 使用 LINQ select 返回异步任务

c# - 有没有办法以相应的形式显示错误?

c# - 内部带有控件的 WPF 装饰器