0

In previous version of asp.net the HostingEnvironment had MapPath method to get and store the path of the file but in ASP.net 5 I can't use it.

var filepath = HostingEnvironment.MapPath(@"~/Data/product.json");
Sestertius
  • 1,367
  • 1
  • 14
  • 13
Avesh Naik
  • 79
  • 1
  • 11
  • You may get an instance of type `Microsoft.AspNet.Hosting.IHostingEnvironment` from DI, it has `MapPath` method. – Ricky Feb 19 '16 at 12:28

2 Answers2

0

Are you using RC1? In RC1 it depends if you are writing a console or web app. In console apps, you cannot use DI anymore but PlatformServices.Default.

PlatformServices.Default.Application gives you access to the base path of your application for example. The type of the static property is IRuntimeEnvironment. And having the base path of your app, you can easily build the path to files you need...

If you are building a web app, you should be able to inject IRuntimeEnvironment to your startup and use it from there.

You have to add a reference to the Microsoft.Extensions.PlatformAbstractions package to all that stuff.

See also my post here for more details

MichaC
  • 13,104
  • 2
  • 44
  • 56
0

Despite IHostingEnvironment provides MapPath() method you may also need UnmapPath() too. Another useful method might be IsPathMapped().

You can find all of them here: Reading a file in MVC 6.

And all of them, thanks to PlatformServices availability, work in Consolse, MVC, and ClassLib apps.

HTH

Community
  • 1
  • 1
Alexander Christov
  • 9,625
  • 7
  • 43
  • 58