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");
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");
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
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