Domanda

Come faccio a supporto Etag in ASP.NET MVC?

È stato utile?

Soluzione

@Elia Glover risposta è una parte della risposta, ma non è davvero completo.Questo imposterà il ETag, ma non stai ricevendo i benefici di Etag senza verificare sul lato server.Si può farlo anche con:

var requestedETag = Request.Headers["If-None-Match"];
if (requestedETag == eTagOfContentToBeReturned)
        return new HttpStatusCodeResult(HttpStatusCode.NotModified);

Inoltre, un altro suggerimento è che è necessario impostare il valore della risposta, altrimenti di default è "privato" e il ETag non sarà impostato nella risposta:

Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);

Così un esempio completo:

public ActionResult Test304(string input)
{
    var requestedETag = Request.Headers["If-None-Match"];
    var responseETag = LookupEtagFromInput(input); // lookup or generate etag however you want
    if (requestedETag == responseETag)
        return new HttpStatusCodeResult(HttpStatusCode.NotModified);

    Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);
    Response.Cache.SetETag(responseETag);
    return GetResponse(input); // do whatever work you need to obtain the result
}

Altri suggerimenti

di ETAG nel MVC sono gli stessi WebForms o gestori HTTP.

Avete bisogno di un modo di creare valore ETAG, il modo migliore che ho trovato sta usando un file MD5 o ShortGuid .

Dal .net accetta una stringa come ETAG, è possibile impostare facilmente usando

String etag = GetETagValue(); //e.g. "00amyWGct0y_ze4lIsj2Mw"
Response.Cache.SetETag(etag);

MIX , alla fine usano ETAG di con il riposo

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top