[英]How to consistently get application base path for ASP.NET 5 DNX project on both production and development environment?
I have deployed a ASP.NET MVC 6 website to Azure from Git. 我已经从Git向Azure部署了一个ASP.NET MVC 6网站。 Details of the deployment can be found in this blog post but basically I use DNU to publish it and then kudu to push it to an Azure website.
有关部署的详细信息可以在这篇博客文章中找到,但基本上我使用DNU发布它,然后kudu将其推送到Azure网站。
Using IHostingEnvironment
I get the ApplicationBasePath. 使用
IHostingEnvironment
我得到了ApplicationBasePath。 The problem is that the paths I get back are very different on localhost and on Azure. 问题是我回来的路径在localhost和Azure上是非常不同的。
Azure: "D:\\home\\site\\approot\\src\\src" Azure:“D:\\ home \\ site \\ approot \\ src \\ src”
Localhost: "C:\\Users\\deebo\\Source\\mysite\\site\\src" Localhost:“C:\\ Users \\ deebo \\ Source \\ mysite \\ site \\ src”
I want to use the base path to get the full path to a folder containing some images: wwwroot/img/gallery/ 我想使用基本路径获取包含一些图像的文件夹的完整路径:wwwroot / img / gallery /
I got around this with the following code: 我用以下代码解决了这个问题:
var rootPath = _appEnvironment.ApplicationBasePath;
var pathFix = "../../../";
if(_hostingEnvironment.IsDevelopment())
{
pathFix = string.Empty;
}
var imagesPath = Path.Combine(rootPath, pathFix, "wwwroot", "img", "gallery");
This may work but seems hacky. 这可能有用,但看起来很黑。
Is it possible my deployment method impacts on this? 我的部署方法是否可能对此产生影响?
Is there a more consistent way to get the application path? 是否有更一致的方法来获取应用程序路径?
You can use the IHostingEnvironment.WebRootPath
. 您可以使用
IHostingEnvironment.WebRootPath
。 From the Asp 5 deep dive : 从Asp 5 深潜 :
The
IHostingEnvironment
services gives you access to the Web root path for your application (typically your www folder) and also an IFileProvider abstraction for your Web root.IHostingEnvironment
服务允许您访问应用程序的Web根路径(通常是您的www文件夹)以及Web根目录的IFileProvider抽象。
So you can get the wwwroot path or even directly map a path: 所以你可以获得wwwroot路径甚至直接映射路径:
var wwwrootPath = env.WebRootPath;
var imagesPath = hostingEnv.MapPath("img/gallery");
PS. PS。 I tried this locally, not on Azure.
我在本地尝试过,而不是在Azure上。 However I haven't found any mention about issues in Azure.
但是我没有发现有关Azure中的问题的任何提及。 This answer even mentions that in Azure
IHostingEnvironment.WebRootPath
will point to D:/Home/site/wwwroot
. 这个答案甚至提到在Azure中
IHostingEnvironment.WebRootPath
将指向D:/Home/site/wwwroot
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.