如何在ASP中支持ETags ?净MVC吗?

[英]How do I support ETags in ASP.NET MVC?


How do I support ETags in ASP.NET MVC?

如何在ASP中支持ETags ?净MVC吗?

2 个解决方案

#1


41  

@Elijah Glover's answer is part of the answer, but not really complete. This will set the ETag, but you're not getting the benefits of ETags without checking it on the server side. You do that with:

@Elijah Glover的回答是答案的一部分,但并不是全部。这将设置ETag,但是如果不在服务器端检查ETags,您将得不到ETags的好处。你这样做:

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

Also, another tip is that you need to set the cacheability of the response, otherwise by default it's "private" and the ETag won't be set in the response:

另外,另一个提示是您需要设置响应的可缓存性,否则默认情况下它是“private”的,并且ETag不会在响应中设置:

Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);

So a full example:

所以一个完整的例子:

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
}

#2


30  

ETAG's in MVC are the same as WebForms or HttpHandlers.

ETAG在MVC中与WebForms或HttpHandlers相同。

You need a way of creating the ETAG value, the best way I have found is using a File MD5 or ShortGuid.

您需要一种创建ETAG值的方法,我发现最好的方法是使用文件MD5或ShortGuid。

Since .net accepts a string as a ETAG, you can set it easily using

由于.net接受一个字符串作为ETAG,所以可以很容易地设置它

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

Video from MIX, at the end they use ETAG's with REST

来自MIX的视频,最后他们使用ETAG的REST

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2009/06/02/eecb985bfbf2ffec3f1eeb0fc9ef2a6a.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告