处理请求时发生未处理的异常。点网核心

时间:2020-11-08 14:20:07

标签: c# asp.net-core

我发现了许多类似的问题,但无法解决此问题,因此发布了一个新问题。

我已经开发了具有登录/注册功能的dotnet核心Web应用程序。在“编辑个人资料”部分中,有一个功能,用户可以在其中更新他的个人资料图片。这在本地非常正常。将其部署到服务器时,出现以下错误。

enter image description here

代码以供参考

        private Dictionary<string, string> UploadProfilePic(EditUserProfilePicViewModel model)
    {
        string uniqueFileName = null;
        Dictionary<string, string> imageNamesMap = new Dictionary<string, string>();

        string contentRoot = config.GetValue<string>(WebHostDefaults.ContentRootKey);
        string tempFolder = contentRoot + "\\wwwroot\\images\\temp";
        string uploadFolder = contentRoot + "\\wwwroot\\images\\profilepics";

        if (model.ProfileImage != null)
        {
            try
            {
                string[] fileNameArray = model.ProfileImage.FileName.ToString().Split('.');
                string fileExtension = fileNameArray[fileNameArray.Length - 1];
                uniqueFileName = model.ProfileId.ToString() + '-' + model.ProfileUrl + "." + fileExtension;

                //store image in the temp folder
                string filePath = Path.Combine(tempFolder, uniqueFileName);

                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    model.ProfileImage.CopyTo(fileStream);
                }

                using (Image sourceImage = Image.FromFile(filePath))
                {
                    if (sourceImage != null)
                    {
                        try
                        {
                            //resize to 300px and store in profilepics folder
                            using (Image destinationImage = ResizeImage(sourceImage, 300, 300))
                            {
                                uniqueFileName = model.ProfileId.ToString() + '-' + model.ProfileUrl + "-300." + fileExtension;
                                imageNamesMap.Add("LargeImage", uniqueFileName);
                                string fileSavePath = Path.Combine(uploadFolder, uniqueFileName);
                                destinationImage.Save(fileSavePath, ImageFormat.Jpeg);
                            }

                            //resize to 75px and store in profilepics folder
                            using (Image destinationImage = ResizeImage(sourceImage, 75, 75))
                            {
                                uniqueFileName = model.ProfileId.ToString() + '-' + model.ProfileUrl + "-75." + fileExtension;
                                imageNamesMap.Add("SmallImage", uniqueFileName);
                                string fileSavePath = Path.Combine(uploadFolder, uniqueFileName);
                                destinationImage.Save(fileSavePath, ImageFormat.Jpeg);
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
                DeleteFilesFromDir(tempFolder);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        return imageNamesMap;
    }

您能帮我吗?

1 个答案:

答案 0 :(得分:0)

该错误表明您的应用程序用于在服务器上运行的用户帐户无权访问上述路径(您可以使用特权帐户登录服务器并启动您的应用程序以确认这一点)。 任何应用程序都是从特定用户帐户运行的系统进程,它可以是真实的本地服务器帐户(SERVERNAME \ user),Active Directory帐户(DIRECTORYNAME \ user)或某些虚拟帐户。如果您使用IIS在服务器上托管应用程序,则很可能它将使用虚拟IIS帐户启动应用程序进程(IIS APPPOOL )。要解决此问题,您需要获取应用程序正在使用的应用程序池名称(例如myapppool),请转到有问题的文件夹的属性安全性选项卡,并为用户'IIS APPPOOL \ myapppool'添加适当的访问权限(以您的情况写)。之后,重新启动应用程序后,便可以在其中创建文件。