How to to return an image with Web API Get method

前端 未结 4 1815
自闭症患者
自闭症患者 2020-12-01 08:57

I need to return an image with a Web API Get method. The code below seems to work fine except that I get this message in the Fiddler\'s ImageView window, \"This response is

4条回答
  •  渐次进展
    2020-12-01 09:36

    If I understand correctly then you are asking specific to asp.net core. In ASP.net core HttpResponseMessage is not a way to return result the way we used to do in ASP.net web api 2.

    In asp.net core ( WEB API ) simply look like this.

    [HttpGet]
    public IActionResult Get()
    {            
        Byte[] b = System.IO.File.ReadAllBytes(@"E:\\Test.jpg");   // You can use your own method over here.         
        return File(b, "image/jpeg");
    }
    

    Note: As you mention that in Fiddler Imageview you see message like this "his response is encoded, but does not claim to be an image." because ASP.net core consider HttpResponseMessage as simple class and convert into json or xml.

提交回复
热议问题