App_Data directory in ASP.NET5 MVC6

我们两清 提交于 2019-12-03 15:01:35

问题


I've been trying ASP.NET5 MVC6 app. In the previous version, there was a directory App_Data. I used this folder to store error logs. But it is not found in latest version. Any help?


回答1:


This works for ASP.NET MVC with Core 2

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// Use this code if you want the App_Data folder to be in wwwroot
//string baseDir = env.WebRootPath;

// Use this if you want App_Data off your project root folder
string baseDir = env.ContentRootPath;

AppDomain.CurrentDomain.SetData("DataDirectory", System.IO.Path.Combine(baseDir, "App_Data"));
}

Now you can put this code where you need it to get your App_Data folder

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



回答2:


I think putting App_Data under wwwroot is a bad idea. With asp.net 5 when we publish/deploy we get 2 folders approot and wwwroot. Any files that are not going to be served by http requests should not live under wwwroot. It would be better for things that previously would go under App_Data folder to live somewhere under approot instead. This related question of how to access files from approot should be of help




回答3:


The App_Data directory can still be used in ASP.NET 5, it just isn't created by default.

Create it under wwwroot. This is the path returned by AppDomain.CurrentDomain.GetData("DataDirectory").ToString()

If you want to use a different DataDirectory then you should call SetData:

  public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
  {
     string baseDir = appEnv.ApplicationBasePath;
     AppDomain.CurrentDomain.SetData("DataDirectory", Path.Combine(baseDir, "myAppData"));


来源:https://stackoverflow.com/questions/31579229/app-data-directory-in-asp-net5-mvc6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!