나는 액션 필터 생성을 가지고 놀고 ETag에 대한 액션 필터를 구현하고 10 초 동안 mvc 4 템플릿에서 HomeController의 캐싱을 만들고 싶습니다.
내 속성은 다음과 같습니다.
public class EtagFilterAttribute : ActionFilterAttribute
{
private DateTime _currentTime = DateTime.Now;
private string _currentEtag = string.Empty;
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var httpContext = filterContext.RequestContext.HttpContext;
string eTag = httpContext.Request.Headers["ETag"];
string responseETag = GetEtag();
if (!string.IsNullOrEmpty(eTag))
{
if (eTag.Equals(responseETag))
{
filterContext.HttpContext.Response.StatusCode = 304;
filterContext.HttpContext.Response.StatusDescription = "Not Modified";
}
return;
}
filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);
filterContext.HttpContext.Response.AddHeader("ETag", responseETag);
filterContext.HttpContext.Response.Cache.SetETag(responseETag);
}
private string GetEtag()
{
if (_currentTime <= DateTime.Now.AddSeconds(10))
{
return _currentEtag;
}
_currentEtag = GenerateEtag();
return _currentEtag;
}
private string GenerateEtag()
{
return Guid.NewGuid().ToString().Substring(0, 20);
}
}
다음은 HomeController입니다.
public class HomeController : Controller
{
[EtagFilter]
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
return View();
}
[EtagFilter]
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
[EtagFilter]
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
컴파일되고 실행되지만 Chrome 개발자 도구 네트워크-> 헤더 탭에 ETag가 표시되지 않습니다.
요청 URL : http : // : 1772 / Home / 요청 방법 정보 : GET 상태 코드 : 200 OK 요청 헤더 소스보기 Accept : text / html, application / xhtml + xml, application / xml; q = 0.9, /; q = 0.8 Accept-Encoding : gzip, deflate, sdch Accept-Language : en-US, en; q = 0.8 Connection : keep-alive Cookie : __ RequestVerificationToken = q0yQrf5ee5bsOW-1OXKK754FeRZM89uNQQ1rvN2cVRXaHs5-PGhOT7sq0uZ5-PGhOTt2zw2cUyFuNQQ1rvN2cVRXaHs5-PGhOTt2zw2cUy 1772 Referer : http : // : 1772 / User-Agent : Mozilla / 5.0 (Windows NT 6.1; WOW64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 29.0.1547.76 Safari / 537.36 Response Headersview source Cache-Control : private Content -Encoding : gzip Content-Length : 956 Content-Type : text / html; charset = utf-8 날짜 : Fri, 04 Oct 2013 15:34:02 GMT Server : Microsoft-IIS / 8.0 Vary : Accept-Encoding X-AspNet-Version : 4.0.30319 X-AspNetMvc-Version : 4.0 X-Powered- 작성자 : ASP.NET X-SourceFiles : =? UTF-8? B? ZDpcTXlEb2N1bWVudHNcR2VuZXJhbFxDI1wuTmV0IE1lbnRvcmluZyBQcm9ncmFtXEhUVFBcRXhhbXBsZEFGV2ZxBsZEFGVIw21
내가 어디에서 착각했는지 말해 줄래?
이것은 작동합니다 :
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var httpContext = filterContext.RequestContext.HttpContext;
string eTag = httpContext.Request.Headers["ETag"];
string responseETag = GetEtag();
if (!string.IsNullOrEmpty(eTag))
{
if (eTag.Equals(responseETag))
{
filterContext.HttpContext.Response.StatusCode = 304;
filterContext.HttpContext.Response.StatusDescription = "Not Modified";
}
return;
}
httpContext.Response.AddHeader("ETag", responseETag);
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다