Jak buforować dane wyjściowe metody działania, która zwraca obraz do widoku w asp.net mvc?
Czytałem już wiele postów na temat buforowania, ale żaden z nich nie odpowiada dokładnie moim potrzebom. W mojej aplikacji mvc 3 mam metodę akcji GetImage (), która zwraca plik typu obrazu. Następnie używam tej metody w celu wyświetlenia obrazu:
<img width="75" height="75" src="@Url.Action("GetImage", "Store", new {productId = item.ProductId})"/>
Chcę buforować obrazy na serwerze. Więc co już próbowałem:
1) aby użyć OutputCacheAttribute:
[HttpGet, OutputCache(Duration = 10, VaryByParam = "productId", Location = OutputCacheLocation.Server, NoStore = true)]
public FileContentResult GetImage(int productId)
{
var p = _productRepository.GetProduct(productId);
if (p != null)
{
if (System.IO.File.Exists(GetFullProductImagePath(productId)))
{
var image = Image.FromFile(GetFullProductImagePath(productId));
return File(GetFileContents(image), "image/jpeg");
}
}
var defaultPath = AppDomain.CurrentDomain.BaseDirectory +
ConfigurationManager.AppSettings["default-images-directory"];
var defaultImage = Image.FromFile(Path.Combine(defaultPath, "DefaultProductImage.jpg"));
return File(GetFileContents(defaultImage), "image/jpeg");
}
Obrazy nie są buforowane (otrzymuję status: 200 OK)
2) użyć następujących metod Response.Cache w metodzie GetImage ():
public FileContentResult GetImage(int productId)
{
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetMaxAge(new TimeSpan(0, 0, 0, 10));
Response.Cache.SetExpires(DateTime.Now.Add(new TimeSpan(0, 0, 0, 10)));
Response.Cache.AppendCacheExtension("must-revalidate, proxy-revalidate");
// other code is the same
}
Obrazy nie są buforowane
3) Tutaj otrzymuję: 304 niezmodyfikowany, ale metoda GetImage () nie zwraca nic (pusty obraz)
public FileContentResult GetImage(int productId)
{
Response.StatusCode = 304;
Response.StatusDescription = "Not Modified";
Response.AddHeader("Content-Length", "0");
// other code is the same
}
Pytanie: Jak buforować dane wyjściowe tej metody działania na serwerze?