我想玩动作过滤器创建并为 ETag 实现动作过滤器,并从 mvc 4 模板缓存 HomeController 10 秒。
这是我的属性:
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 开发者工具 Network->Headers 选项卡中我没有看到 ETag:
请求地址:http://:1772/Home/关于请求方式:GET 状态码:200 OK 请求头查看源接受: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-1OXKK754FeRZM89uNQQ1rvN2cVRXaHsPGhOTt2zw2cUyfmu-0uZGLL5-ebs-iJH1-RQ3Q7qb6z4jTHNY8yGJQ3KBYhRs1 Host:localhost: 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 -编码:gzip 内容长度:956 内容类型:文本/html;charset=utf-8 日期:2013 年 10 月 4 日星期五 15:34:02 GMT 服务器:Microsoft-IIS/8.0 Vary:Accept-Encoding X-AspNet-Version:4.0.30319 X-AspNetMvc-Version:4.0 X-Powered- By:ASP.NET X-SourceFiles:=?UTF-8?B?ZDpcTXlEb2N1bWVudHNcR2VuZXJhbFxDI1wuTmV0IE1lbnRvcmluZyBQcm9ncmFtXEhUVFBcRXhhbXBsZVxIdHRwRm9yRGV2ZWxvcGVyc1xIb21lXEFib3V0?=
你能告诉我我哪里错了吗?