從ASP.NET Core RC2開始,可以通過注入 IHostingEnvironment 服務對象來取得Web根目錄和內容根目錄的物理路徑,如下所示:
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
namespace AspNetCorePathMapping
{
public class HomeController : Controller
{
private readonly IHostingEnvironment _hostingEnvironment;
public HomeController(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}
public ActionResult Index()
{
var Path1 = _hostingEnvironment.ContentRootPath;//D:\項目\數據可視化平臺\SVN\DataVisualization
var Path2 = _hostingEnvironment.WebRootPath;//D:\項目\數據可視化平臺\SVN\DataVisualization\wwwroot
var Path3 = _hostingEnvironment.WebRootFileProvider;
var Path4 = _hostingEnvironment.EnvironmentName;
var Path5 = _hostingEnvironment.ApplicationName;//DataVisualization
var Path6 = _hostingEnvironment.ContentRootFileProvider;
var Path7 = _hostingEnvironment.IsDevelopment();
var Path8 = _hostingEnvironment.IsEnvironment("123");
var Path9 = _hostingEnvironment.IsProduction();
var Path10 = _hostingEnvironment.IsStaging();
return null;
}
}
}
其他幾個屬性點進去看詳解吧,我使用不上就沒有詳細瞭解。