ASPX返回一个图片 - 输出缓存能? [英] ASPX That Returns An Image - Output Cache-able?
问题描述
您好!
我已经创建了返回基于某些提供的参数的远程图像的APSX Web表单。它可以像这样使用:
I've created an APSX web form that returns a remote image based on some supplied parameters. It can be used like this:
<img src="/ImageGetter.aspx?param1=abc¶m2=123" />
ImageGetter.aspx的标记和code类似于此:
ImageGetter.aspx's markup and code look similar to this:
<%@ OutputCache Duration="100000" VaryByParam="*" Location="ServerAndClient" %>
<%@ Page Language="C#" AutoEventWireup="false" EnableSessionState="False" CodeBehind="ImageGetter.aspx.cs" Inherits="ACME.Helpers.ImageGetter" %>
这code被称为ImageGetter.aspx的Page_Load方法:
This code is called in ImageGetter.aspx's Page_Load method:
byte[] data = null;
Dictionary<string, string> file_locations = GetImageLocations(param1, param2);
try
{
data = new WebClient().DownloadData(file_locations["main"]);
}
catch (WebException wex)
{
try
{
data = new WebClient().DownloadData(file_locations["backup"]);
}
catch (Exception e)
{
throw;
}
}
Response.ContentType = "image/jpeg";
Response.OutputStream.Write(data, 0, data.Length);
Response.End();
从我的测试,它不会出现被缓存。这是可以做到与输出缓存或者我应该诉诸写我自己的高速缓存来存储字节数组根据查询字符串paramters?
From my testing, it doesn't appear to be caching. Is this possible to do with Output Caching or should I resort to writing my own cache to store the byte arrays depending on query string paramters?
推荐答案
尝试下探到Response.End(),因为这将pmaturely终止线程$ P $和prevent输出缓存的发生。
Try dropping the Response.End() as this will terminate the thread prematurely and prevent output caching from taking place.
请参阅:<一href=\"http://bytes.com/groups/net-asp/323363-cache-varybyparam-doesnt-work\">http://bytes.com/groups/net-asp/323363-cache-varybyparam-doesnt-work
您可能考虑使用一个ASHX处理程序,并使用自己的高速缓存方法。
You may wish to consider using an ASHX handler and using your own caching method.
这篇关于ASPX返回一个图片 - 输出缓存能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!