Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 284
xakpc Avatar asked Dec 31 '25 23:12

xakpc


1 Answers

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;
}
like image 131
Sirwan Afifi Avatar answered Jan 02 '26 13:01

Sirwan Afifi