2013-02-08 16 views
10

Zaten önbelleğe alma hakkında birçok yazı okudum, ancak bunların hiçbiri tam olarak gereksinimlerimle eşleşmiyor. Benim mvc 3 uygulamasında bir görüntü tipi Dosya döndüren GetImage() bir eylem yöntemi var. Sonra resmi görüntülemek için bir görünümde bu yöntemi kullanmak:Asp.net mvc'deki görüntüye görüntüyü döndüren eylem yönteminin çıktısı nasıl önlenir?

<img width="75" height="75" src="@Url.Action("GetImage", "Store", new {productId = item.ProductId})"/> 

Bir Server resimleri önbelleğe istiyorum. Yani, zaten ne denedim:

1) OutputCacheAttribute kullanmak: 200 OK)

2) Aşağıdaki Yanıtı kullanmalarını:

[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"); 
    } 

Görüntüler önbelleğe alınmaz (ı durumunu olsun. bir GetImage() yönteminde Önbellek yöntemleri: Görüntüler önbelleğe alınmaz

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 
    } 

) Onun 304 Modifiye değil, ama GetImage() yöntemi bir şey (boş görüntü)

public FileContentResult GetImage(int productId) 
    { 
     Response.StatusCode = 304; 
     Response.StatusDescription = "Not Modified"; 
     Response.AddHeader("Content-Length", "0");  
     // other code is the same 
    } 

döndürür Soru: E alıyorum nasıl bir sunucuda bu eylem yönteminin çıkışını önbelleğe? OutputCacheLocation.ServerAndClient kullanarak ve NoStore = true elden çıkartıldı:

[HttpGet] 
[OutputCache(
    Duration = 10, 
    VaryByParam = "productId", 
    Location = OutputCacheLocation.ServerAndClient)] 
public ActionResult GetImage(string productId) 
{ 
    ... 
} 

şeyler fark:

cevap

13

böyle deneyin.

+0

Teşekkürler, mükemmel çalışıyor! Ama bunu anlamıyorum: Resmi bir sunucuda veya istemcide ayrı ayrı önleyemiyorum? –

+1

Ancak, görüntüyü yalnızca sunucuda önbelleğe aldığınızda, bu sunucu ** DAİMA ** isabetlidir. İstemci görüntüyü önbelleğe almamış ve bunun bir kopyası yok. Müşterinin bu görüntüden nereden alacağını düşünüyorsun? Bu yüzden 200 durum kodu aldınız. Fark, pahalı denetleyici eyleminizin sunucuda çalıştırılmamasıdır, ancak görüntü doğrudan önbellekten istemciye gönderilir. –

+0

Burada ihtiyacım var VaryByParam = "ProductId"? Sonuç etkilemiyor gibi mi görünüyor? Ve, aldığım gibi, "ServerAndClient", tüm kullanıcıların önbelleğe alınmış aynı görüntüleri alacağı anlamına geliyor? –

İlgili konular