简体   繁体   中英

How do i get bin or wwwroot directory in ASP.NET MVC 6 (SIX) code

In ASP.NET MVC 5 I used

_myStem = new MyStemWrapper(Path.Combine(HttpRuntime.AppDomainAppPath, "mystem.exe"));

to get path to file.

In ASP.NET MVC 6 I can't get path by the same way. I need to get path before any requests since my wrapper is a singleton created by IOC

var d = AppDomain.CurrentDomain; // some path to C:\Users\username\.dnx\runtimes\dnx-clr-win-x86.1.0.0-beta5\bin
var r = HttpRuntime.BinDirectory; // ArgumentNullException
var r2 = HttpRuntime.AppDomainAppPath; // ArgumentNullException
var ap = Apllication.StartupPath; // Application class missing
var s = Server.MapPath("~/") // Server class missing

In ASP.NET 5 you can easily access these information using IApplicationEnvironment :

private readonly IApplicationEnvironment _app;
public HomeController(IApplicationEnvironment app)
{
   _app = app;
}
public IActionResult Index()
{
   var path = _app.ApplicationBasePath;
}

Update : If you want exactly the wwwroot path, You can inject IHostingEnvironment and get WebRootPath property:

private readonly IHostingEnvironment _app;
public HomeController(IHostingEnvironment app)
{
     _app = app;
}
public IActionResult Index()
{
    var path = _app.WebRootPath;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM