示例如下:
[HandleError]
public class CacheHandlerController : Controller
{
public ActionResult Index()
{
return View();
}
[OutputCache(Location = System.Web.UI.OutputCacheLocation.None)]
public ActionResult RenderWithOutCache()
{
ViewData["Now"] = DateTime.Now;
return View("RenderCache");
}
[OutputCache(Duration = 5, VaryByParam = "none")]
public ActionResult RenderCache()
{
ViewData["Now"] = DateTime.Now;
return View();
}
[OutputCache(Duration = 9999, VaryByParam = "none", SqlDependency = "Demo:UserInfo")]
public ActionResult SqlCacheDependency()
{
ViewData["Now"] = DateTime.Now;
return View("RenderCache");
}
}
自定义过滤器 1.ASP.Net MVC内置四类过滤器分别为: IAuthorizationFilter IActionFilter IResultFilter IExceptionFilter 2.关于过滤器的说明上述过滤器在使用上存在优先级,且依序从高到低可通过实现对应的接口创建自定义的过滤器 3.自定义过滤器,示例(...) 示例如下 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class TraceAttribute : FilterAttribute, IActionFilter, IResultFilter
{
#region Properties
public string LogName { get; set; }
#endregion
#region IActionFilter Members
public void OnActionExecuting(ActionExecutingContext filterContext)
{
LogMessage(
filterContext.RouteData.Values["controller"].ToString(),
filterContext.RouteData.Values["action"].ToString(),
"Action executing..."
);
}
public void OnActionExecuted(ActionExecutedContext filterContext)
{
LogMessage(
filterContext.RouteData.Values["controller"].ToString(),
filterContext.RouteData.Values["action"].ToString(),
"Action executed."
);
}
#endregion
#region IResultFilter Members
public void OnResultExecuting(ResultExecutingContext filterContext)
{
LogMessage(
filterContext.RouteData.Values["controller"].ToString(),
filterContext.RouteData.Values["action"].ToString(),
"Result executing..."
);
}
public void OnResultExecuted(ResultExecutedContext filterContext)
{
LogMessage(
filterContext.RouteData.Values["controller"].ToString(),
filterContext.RouteData.Values["action"].ToString(),
"Result executed."
);
}
#endregion
#region Helper methods
private void LogMessage(string controller, string action, string message)
{
......
}
#endregion
}
|
请发表评论