An Image/PDF file as response

It's a common requirement in most of the applications to retrieve a PDF or image file as response. It's quite easy to implement this scenario in Web API by writing a couple of lines of code in your action method to override the default media type of the response determined by Content Negotiation.

Let's open up the ValuesController.cs file, and add one more action method called GetImage(), which will return an image file in the Content folder of the service as shown:

public HttpResponseMessage GetImage()
{
    byte[] bytes = System.IO.File.ReadAllBytes(
        HttpContext.Current.Server
        .MapPath("~/Content/Kendo.png"));
    var result = new HttpResponseMessage(HttpStatusCode.OK);
    result.Content = new ByteArrayContent(bytes);
    result.Content.Headers.ContentType ...

Get Building Mobile Applications Using Kendo UI Mobile and ASP.NET Web API now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.