C# 从WebApi获取图像错误HttpResponseMessage

C# 从WebApi获取图像错误HttpResponseMessage,c#,asp.net-web-api,httpresponsemessage,C#,Asp.net Web Api,Httpresponsemessage,嗨,我在Web Api服务中有获取个人图像的方法 public HttpResponseMessage GetPersonnelPicture( int personnelId) { JamsazERPLiteEntities db = new JamsazERPLiteEntities(); var fingerPrint = db.FingerPrints.FirstOrDefault(c => c.SequenceNumber == fin

嗨,我在Web Api服务中有获取个人图像的方法

 public HttpResponseMessage GetPersonnelPicture( int personnelId)
    {
        JamsazERPLiteEntities db = new JamsazERPLiteEntities();

        var fingerPrint = db.FingerPrints.FirstOrDefault(c => c.SequenceNumber == fingerNumber && c.MAC == MAC && !c.IsDeleted);

        if (fingerPrint != null && fingerPrint.Personnel != null)
        {
            try
            {
                var result = new HttpResponseMessage(HttpStatusCode.OK);
                FileStream fileStream =
                    new FileStream(@"\\atlas\Personnel Pictures\" + personnelId + ".jpg",
                        FileMode.Open);
                Image image = Image.FromStream(fileStream);
                MemoryStream memoryStream = new MemoryStream();
                image.Save(memoryStream, ImageFormat.Jpeg);
                result.Content = new ByteArrayContent(memoryStream.ToArray());
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
                memoryStream.Close();
                memoryStream.Dispose();
                fileStream.Close();
                fileStream.Dispose();

                return result;
            }
            catch { }
        }

        return null;
    }
在IIS Express中在visual studio 2017中运行Web服务并成功从客户端发送请求时(我通过邮递员应用程序从发送请求
localhost:23254/api/Weight/GetPersonnelPicture/?personnelId=2154进行测试)

但当发布到本地IIS时,邮递员应用程序会出现流动错误

{ “消息”:“发生错误。”, “ExceptionMessage”:“在预期HttpResponseMessage实例的位置返回空值。”, “异常类型”:“System.InvalidOperationException”, “StackTrace”:“at System.Web.Http.Controllers.ResponseMessageResultConverter.Convert(HttpControllerContext controllerContext,对象actionResult)\r\n位于 System.Web.Http.Controller.ApiControllerActionInvoker.d\u 0.MoveNext()\r\n--- 来自引发异常的上一个位置的堆栈结束跟踪 ---\r\n位于System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n
在 System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\r\n位于 System.Web.Http.Controllers.ActionFilterResult.d\u 2.MoveNext()\r\n--- 来自引发异常的上一个位置的堆栈结束跟踪 ---\r\n位于System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n
在 System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\r\n位于 System.Web.Http.Dispatcher.HttpControllerDispatcher.d_u0.MoveNext() }


您需要先定义httpwebrequest,然后才能从http服务器获得响应

         HttpWebRequest request = (HttpWebRequest)WebRequest.Create("httpurl");
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                      //your code
                    }
                }

您需要先定义httpwebrequest,然后才能从http服务器获得响应

         HttpWebRequest request = (HttpWebRequest)WebRequest.Create("httpurl");
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                      //your code
                    }
                }
在IIS 7(不是IIS 7.5)中,站点根据在站点的应用程序池上设置的帐户访问文件和文件夹。默认情况下,在IIS7中,此帐户是网络服务

指定应用程序池(IIS 7)的标识

在IIS 7.5(Windows 2008 R2和Windows 7)中,应用程序池在应用程序池启动时创建的ApplicationPoolIdentity下运行。如果要为此帐户设置ACL,则需要选择IIS AppPool\ApplicationPoolName,而不是NT Authority\Network Service。

在IIS 7(不是IIS 7.5)中,站点根据在站点的应用程序池中设置的帐户访问文件和文件夹。默认情况下,在IIS7中,此帐户是网络服务

指定应用程序池(IIS 7)的标识

在IIS 7.5(Windows 2008 R2和Windows 7)中,应用程序池在应用程序池启动时创建的ApplicationPoolIdentity下运行。如果要为此帐户设置ACL,则需要选择IIS AppPool\ApplicationPoolName,而不是NT Authority\Network Service。

在catch块中添加一些错误日志代码。您似乎遇到了一些错误并进入catch部分。我认为您的站点在IIS中运行时所使用的标识可能无法访问该文件共享。您的应用程序运行时所使用的应用程序池具有标识。如果您没有设置它,那么它将是默认的应用程序池标识,对任何外部内容都没有权限。您可以将标识更改为更合适的标识,但如果不小心,您可以赋予太多的权限。你们有系统/基础设施部门吗?。。。但是首先要捕获并检查异常。不要试图解决您不知道的问题。感谢您回答如何在iis中添加站点访问图像文件夹的权限?我和Atlas上的图像文件夹可以从本地计算机访问到图像文件夹。iis在本地计算机上运行。您需要添加权限吗?在catch块中添加一些错误日志代码。您似乎遇到了一些错误并进入catch部分。我认为您的站点在IIS中运行时所使用的标识可能无法访问该文件共享。您的应用程序运行时所使用的应用程序池具有标识。如果您没有设置它,那么它将是默认的应用程序池标识,对任何外部内容都没有权限。您可以将标识更改为更合适的标识,但如果不小心,您可以赋予太多的权限。你们有系统/基础设施部门吗?。。。但是首先要捕获并检查异常。不要试图解决您不知道的问题。感谢您回答如何在iis中添加站点访问图像文件夹的权限?我和Atlas上的图像文件夹可以从本地计算机访问到图像文件夹。iis在本地计算机上运行。您需要添加权限吗?对于您的答案,我使用此代码获取图像PersonalImage=new BitmapImage(新Uri([IIS Ip]/api/Weight/GetPersonnelPicture/?personnelId=2154));我的应用程序平台是Iot Core Raspberry PiI中的UWP。对于您的答案,我使用此代码获取图像PersonalImage=new BitmapImage(新Uri([IIS Ip]/api/Weight/GetPersonnelPicture/?personnelId=2154));我的应用程序平台是物联网核心Raspberry PiI中的UWP