1

I want intercept all request for image files, but i am not getting a way to do it,

One way is to create an action in MVC controller and return all image files from it,

 public HttpResponseMessage Get(Guid id)
    {
        var path = HttpContext.Current.Server.MapPath(string.Format("/OriginalImages/{0}.jpg", id));
        byte[] fileData = File.Exists(path) ? File.ReadAllBytes(path) : new byte[0];
        MemoryStream ms = new MemoryStream(fileData);
        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
        result.Content = new ByteArrayContent(ms.ToArray());
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
        return  result;
    }

but it does not seems good idea as i noted that image access request completing in 15-20 milliseconds, while direct image access via path takes 5-6 milliseconds.

I tried using Owin Middleware, but it do not get executed on image file requests.

How can i achieve this? I want to increment view count of image file by this.

Vikas Nokhwal
  • 113
  • 10
  • 2
    You might instead edit this to ask, "How can i count image views" and see what people propose instead of asking about intercepting requests, which may not be your answer, and only mentioning what you really want to do in passing. Ask what you want to know rather than what you think the answer should be. – casey Mar 12 '16 at 20:08
  • Use base controller File method as in this post :http://stackoverflow.com/questions/186062/can-an-asp-net-mvc-controller-return-an-image. – KavehG Mar 13 '16 at 04:56

1 Answers1

0

You would have to configure IIS (either through the IIS management interface or the application web.conifg) so it maps service all requests for images through ASP.NET. Then write a handler that will do whatever you want when the image is requested.

Ben Richards
  • 3,437
  • 1
  • 14
  • 18