【问题标题】:App_Data directory in ASP.NET5 MVC6ASP.NET5 MVC6 中的 App_Data 目录
【发布时间】:2015-07-23 05:57:09
【问题描述】:

我一直在尝试 ASP.NET5 MVC6 应用程序。在之前的版本中,有一个目录App_Data。我用这个文件夹来存储错误日志。但在最新版本中找不到。有什么帮助吗?

【问题讨论】:

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


    【解决方案1】:

    这适用于带有 Core 2 的 ASP.NET MVC

    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"));
    }
    

    现在你可以把这段代码放在你需要的地方来获取你的 App_Data 文件夹

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

    【讨论】:

      【解决方案2】:

      我认为将 App_Data 放在 wwwroot 下是个坏主意。 使用 asp.net 5,当我们发布/部署时,我们会得到 2 个文件夹 approot 和 wwwroot。 任何不会被 http 请求提供服务的文件都不应该存在于 wwwroot 下。 以前会放在 App_Data 文件夹下的东西放在 approot 下的某个地方会更好。 这个related question of how to access files from approot should be of help

      【讨论】:

      • 请记住,AppDomain.CurrentDomain.GetData("DataDirectory") 仍将返回 wwwroot\App_Data,除非您更改它
      【解决方案3】:

      App_Data 目录仍然可以在 ASP.NET 5 中使用,只是默认情况下不会创建它。

      wwwroot 下创建它。这是AppDomain.CurrentDomain.GetData("DataDirectory").ToString()返回的路径

      如果您想使用不同的 DataDirectory,则应致电 SetData:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-09
        • 1970-01-01
        • 2016-05-06
        • 2016-10-04
        • 1970-01-01
        相关资源
        最近更新 更多