如何始终如一地得到有关生产和开发环境ASP.NET 5 DNX项目申请基本路径? [英] How to consistently get application base path for ASP.NET 5 DNX project on both production and development environment?

查看:208
本文介绍了如何始终如一地得到有关生产和开发环境ASP.NET 5 DNX项目申请基本路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经部署了ASP.NET MVC 6网站从SVN到Azure。部署的详细信息可在这个博客帖子但基本上我用DNU来发布它,然后捻来推到Azure的网站。

I have deployed a ASP.NET MVC 6 website to Azure from Git. 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.

使用 IHostingEnvironment 我得到的ApplicationBasePath。问题是,我回来的路径是在本地主机上,并在Azure上有很大不同。

Using IHostingEnvironment I get the ApplicationBasePath. The problem is that the paths I get back are very different on localhost and on Azure.

天青:D:\\家\\网站\\为approot的\\ src \\ SRC结果
本地主机:C:\\用户\\ deebo \\来源\\ mysite的\\网站\\ SRC

Azure: "D:\home\site\approot\src\src"
Localhost: "C:\Users\deebo\Source\mysite\site\src"

我想使用的基本路径去包含一些图片的文件夹的完整路径:wwwroot文件/ IMG /画廊/

I want to use the base path to get the full path to a folder containing some images: wwwroot/img/gallery/

我解决这个得到了与以下code:

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?

推荐答案

您可以使用<$c$c>IHostingEnvironment.WebRootPath.从ASP 5 深潜

IHostingEnvironment 服务,您可以访问到Web根路径
  为您的应用程序(通常是您的www文件夹),也是一个
  IFileProvider抽象为您的Web根目录下。

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.

所以,你可以得到的wwwroot路径甚至直接映射路径:

So you can get the wwwroot path or even directly map a path:

var wwwrootPath = env.WebRootPath;
var imagesPath = hostingEnv.MapPath("img/gallery");

PS。我想这在本地,而不是在Azure上。不过,我还没有发现有关在Azure中问题的任何提及。 这个答案甚至提到,在Azure中 IHostingEnvironment.WebRootPath 将指向 D:/首页/网站/ wwwroot文件

PS. I tried this locally, not on Azure. However I haven't found any mention about issues in Azure. This answer even mentions that in Azure IHostingEnvironment.WebRootPath will point to D:/Home/site/wwwroot.

这篇关于如何始终如一地得到有关生产和开发环境ASP.NET 5 DNX项目申请基本路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆