c# – ASP.Net vNext App_Data文件夹

前端之家收集整理的这篇文章主要介绍了c# – ASP.Net vNext App_Data文件夹前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
类似的问题发现在这里: ASP.NET MVC – Find Absolute Path to the App_Data folder from Controller.

App_Data文件夹消失了吗? Server.MapPath似乎也消失了.

我试图用Url.Content实现相同的结果,但它似乎没有起作用.

解决方法

我们在vNext中有App_Data.

这应该仍然有效

string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();

对于Server.MapPath等价物,您可以使用AppDomain.CurrentDomain.BaseDirectory并从那里构建路径.

您还可以使用IApplicationEnvironment服务

private readonly IApplicationEnvironment _appEnvironment;

public HomeController(IApplicationEnvironment appEnvironment)
{
    _appEnvironment = appEnvironment;
}

public IActionResult Index()
{
    var rootPath = _appEnvironment.ApplicationBasePath;
    return View();
}

IHostingEnvironment是Web应用程序的IApplicationEnvironment的道德等价物.对于PhysicalFileSystem,IHostingEnvironment回退到IApplicationEnvironment.

private readonly IHostingEnvironment _hostingEnvironment;

public HomeController(IHostingEnvironment hostingEnvironment)
{
    _hostingEnvironment = hostingEnvironment;
}

public IActionResult Index()
{
   var rootPath = _hostingEnvironment.MapPath("APP_DATA");
   return View();
}

猜你在找的C#相关文章