ASP.Net vNext App_Data文件夹

时间:2014-12-10 11:02:45

标签: c# asp.net asp.net-mvc asp.net-core

与此处发现的问题相似:ASP.NET MVC - Find Absolute Path to the App_Data folder from Controller

App_Data文件夹是否已删除? Server.MapPath似乎也消失了。

我尝试使用Url.Content获得相同的结果,但它似乎无法正常工作。

2 个答案:

答案 0 :(得分:8)

我们在 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网络应用程序 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();
}

答案 1 :(得分:4)

MapPath存在于IHostingEnvironment

private readonly IHostingEnvironment _env;
public HomeController(IHostingEnvironment env)
{
    _env = env;
}

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