public void ProcessRequest(HttpContext context)
        {
            Guid ebookId;
            int position;
            if (context.Request.QueryString.AllKeys.Contains("ebook")
                && Guid.TryParse(context.Request.QueryString["ebook"], out ebookId)
                && context.Request.QueryString.AllKeys.Contains("position")
                && int.TryParse(context.Request.QueryString["position"], out position))
            {
                using (var db = new EbookManagerDbContext())
                {
                    var catalogRepository = new CatalogRepository(db);
                    var ebookPart = catalogRepository.GetEbookPart(ebookId, position);
                    if (ebookPart != null)
                    {
                        context.Response.Clear();
                        context.Response.ContentType = ebookPart.ContentType;
                        context.Response.AddHeader("Content-Disposition", "attachment; filename=" + ebookPart.FileName);
                        context.Response.BinaryWrite(ebookPart.PartContent);
                        context.Response.End();
                        return;
                    }
                }
            }

            context.Response.StatusCode = 404;
            context.Response.StatusDescription = "Not Found";
            context.Response.End();
        }
 public async Task<HttpResponseMessage> GetEbookPart(Guid ebookId, int index)
 {
     using (var db = new EbookManagerDbContext())
     {
         var catalogRepository = new CatalogRepository(db);
         var part = catalogRepository.GetEbookPart(ebookId, index);
         var response = new HttpResponseMessage(HttpStatusCode.OK);
         response.Content = new ByteArrayContent(part.PartContent);
         response.Content.Headers.ContentLength = part.PartContent.Length;
         response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(part.ContentType);
         return response;
     }
 }