ASP.NET MVC에서 ETAGS를 어떻게 지원합니까?
-
06-09-2019 - |
문제
ASP.NET MVC에서 ETAGS를 어떻게 지원합니까?
해결책
@elijah Glover의 대답 답의 일부이지만 실제로는 완전하지는 않습니다. 이렇게하면 ETAG가 설정되지만 서버 측에서 확인하지 않고 ETAG의 이점을 얻지 못합니다. 당신은 다음과 같이합니다.
var requestedETag = Request.Headers["If-None-Match"];
if (requestedETag == eTagOfContentToBeReturned)
return new HttpStatusCodeResult(HttpStatusCode.NotModified);
또한 또 다른 팁은 응답의 캐시 가능성을 설정해야한다는 것입니다. 그렇지 않으면 기본적으로 "개인"이며 ETAG는 응답에 설정되지 않습니다.
Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);
그래서 전체 예 :
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
}
제휴하지 않습니다 StackOverflow