我想拦截所有对图像文件的请求,但我没有办法这样做,
一种方法是在MVC控制器中创建动作并返回其中的所有图像文件,
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;
}但这似乎不是一个好主意,因为我注意到,图像访问请求在15-20毫秒内完成,而通过path直接访问图像需要5-6毫秒。
我尝试使用Owin中间件,但它不能在图像文件请求上执行。
我如何才能做到这一点?我想通过这个来增加图像文件的视图计数。
发布于 2016-03-13 07:22:38
您必须配置IIS (通过IIS管理接口或应用程序web.conifg),以便它通过ASP.NET映射服务对图像的所有请求。然后编写一个处理程序,它将在请求图像时执行您想要的任何操作。
https://stackoverflow.com/questions/35962541
复制相似问题