12

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.

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

Azure: "D:\home\site\approot\src\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/

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?

Devon Burriss
  • 2,497
  • 1
  • 19
  • 21
  • What do you want to do with the application path? How does it influence your application? – Paul Turner Sep 17 '15 at 13:09
  • I want to use the base path to get the full path to a folder containing some images: wwwroot/img/gallery. Added this to question. The path can also be seen in the code. Thanks for helping me make my question clearer – Devon Burriss Sep 17 '15 at 13:17

1 Answers1

20

You can use the IHostingEnvironment.WebRootPath. From the Asp 5 deep dive:

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.

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

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

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.

Community
  • 1
  • 1
Daniel J.G.
  • 34,266
  • 9
  • 112
  • 112
  • Thanks this was exactly what I was looking for. Worked across Windows and Mac localhost as well as on Azure. – Devon Burriss Sep 17 '15 at 14:12
  • Good, and great to know it works on every environment! – Daniel J.G. Sep 17 '15 at 14:13
  • 4
    Great info; (at least as of ASP.NET Core 1.0.0-RC2) you can obtain a reference to an `IHostingEnvironment` instance from any controller by declaring that controller's constructor with a parameter of this type; e.g., `public HomeController(IHostingEnvironment env) { ...` – mklement0 May 31 '16 at 19:59
  • This is also how you would obtain the application's `ContentRootPath`, for anything you are packaging outside the publicly accessible `WebRootPath`. As mklement0 mentions, Dependency Injection is a great way to obtain the `IHostingEnvironment`, which also includes info like whether the app is running in Dev or Prod. – brichins Sep 15 '16 at 17:20