【问题标题】:How to fix server error 500 web api in mobile app如何在移动应用程序中修复服务器错误 500 web api
【发布时间】:2018-10-13 09:19:37
【问题描述】:

我有允许用户上传图片的 ASP.NET WEB API 项目,然后在另一个页面中查看这些图片。
图片上传成功;但是,当从移动应用程序调用 GET 方法时,其中一些图像会返回 SERVER ERROR 500

如果我复制返回错误的 url 并执行来自浏览器或邮递员的请求,则 url 可以正常工作,因此可以在服务器中找到图像。
该网址仅在移动设备上不起作用的原因可能是什么?
这是返回图像的代码:

    [HttpGet]
    [Route("Stores/{id}/Photos/{pid}")]
    public HttpResponseMessage GetStorePhoto(int id, int pid)
    {
        using (var db = new ApplicationDbContext())
        {
            var photo = store.Photos.FirstOrDefault(p => p.Id == pid);
            if (photo == null)
                return ImageNotFound();  //returns a default image in case image not found

            var filePath =
            HttpContext.Current.Server.MapPath("~/Uploads/Stores/" + photo.FileName);

            if (!File.Exists(filePath))
                return ImageNotFound();
            var response = new HttpResponseMessage();
            response.Content = new StreamContent(new FileStream(filePath, FileMode.Open));
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");

            return response;
        }
    }

【问题讨论】:

  • 你的服务器日志说什么?在大多数情况下,错误将在那里打印出来。
  • 错误只是说'500 Internal Server Error'
  • 您能告诉我们您负责返回图像的代码吗?
  • 请检查我更新的问题
  • 您是否尝试过使用 Visual Studio 的 断点 来验证输出?

标签: asp.net azure asp.net-web-api mobile


【解决方案1】:

错误出现在 getRequest 我试图同时访问同一个文件夹两次以获取照片的宽度和高度。删除该代码后,错误已修复。
这是我在获取图像之前调用的代码:

            foreach (var photo in Photos)
            {
                try
                {
                    Image img = Image.FromFile(HttpContext.Current.Server.MapPath("~/Uploads/Stores/" + photo.FileName));
                    var sizedImage = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb);
                    photo.Height = sizedImage.Height;
                    photo.Width = sizedImage.Width;

                }
                catch (Exception Ex)
                {
                    continue;
                }
            }

谢谢!

【讨论】:

    猜你喜欢
    • 2013-03-28
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    相关资源
    最近更新 更多