【问题标题】:Access to path is denied, Asp.net core 2.0 running on Ubuntu访问路径被拒绝,在 Ubuntu 上运行 Asp.net core 2.0
【发布时间】:2018-03-10 15:40:39
【问题描述】:

我无法使用下面的代码行。在我的开发机器(Windows)上它正在工作。是的,我正在 Windows 上开发应用程序并在 Ubuntu 上进行部署。下一个应用我不会这样做。

我收到错误消息“访问路径 /var/... 已定义”。

try
{
   Directory.CreateDirectory(dirInfo.FullName + "/" + numDirs);
}
catch(Exception e)
{
   return e.Message; // access to the path /var/... is defined
}

我使用 nginx 作为 Kestrel 的代理服务器。如Microsoft guide中所述

我尝试随机触发一些权限命令,因为我远非 Ubuntu 专家,但 CreateDirectory 方法仍在产生错误。

我尝试过的权限命令:

sudo chown -R www-data:www-data /var/www/PROJECTDIR

sudo find /var/PROJECTDIR -type d -exec chmod 770 {} \;
sudo find /var/PROJECTDIR -type f -exec chmod 660 {} \;

我没有在 /var/www 中设置我的项目我正在使用 /var/anotherdir/anotherdir 之类的东西,这是一个问题吗?

【问题讨论】:

    标签: c# ubuntu nginx asp.net-core kestrel


    【解决方案1】:

    实际上引发异常的是我的 IFormFile 扩展方法。我尝试发布尽可能少的代码以使问题更紧凑,但我认为这不再是一个好主意,我应该在我的项目中发布 try catch 块。在我的代码中,我有这样的东西。 obs。我只是在测试,最后我摆脱了文字和过多的变量引用。

        try
    {
       Directory.CreateDirectory(dirInfo.FullName + "/" + numDirs);
       file.SaveAs(dirInfo.FullName + "/" + numDirs) // it was this that threw the exception.
    }
    catch(Exception e)
    {
       return e.Message; // access to the path /var/... is defined
    }
    

    这是我遇到过的最烦人的错误之一。幸运的是,我在post 中遇到了一个确切的问题,解决了我的问题。我只是传递目录。

    不知何故,它在 Windows 上可以工作,但在 Linux 上却不行。

     public static void SaveAs(this IFormFile formFile, string filePath)
    {
        using (var stream = new FileStream( filePath, FileMode.Create))
        {
            formFile.CopyTo(stream);
        }
    
    }
    

    解决办法:

     public static void SaveAs(this IFormFile formFile, string filePath)
        {
            using (var stream = new FileStream( Path.Combine( filePath, formFile.FileName), FileMode.Create))
            {
                formFile.CopyTo(stream);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2017-12-12
      • 2013-12-15
      • 1970-01-01
      • 1970-01-01
      • 2017-03-13
      • 2016-11-11
      • 1970-01-01
      相关资源
      最近更新 更多