• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ASP.NETMVC——OutputCache

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
示例程序
1.在Action上标记启用缓存,如:[OutputCache(Duration = 5, VaryByParam = 'none')]
2.在Action上标记禁用本地缓存,如:[OutputCache(Location = System.Web.UI.OutputCacheLocation.None)]
3.在Action上标记使用缓存依赖,如:[OutputCache(Duration = 9999, VaryByParam = 'none', SqlDependency ='Demo:UserInfo')]
使用缓存依赖的要点
1.在Web.config中添加数据库连接字符串,如下:
<connectionStrings>
..<add name="DemoConn" connectionString="server=.\SQLEXPRESS;database=Demo;uid=sa;pwd=123456" providerName="System.Data.SqlClient"/>
</connectionStrings>
2.在Web.config中添加缓存配置,如下:
<caching>
..<sqlCacheDependency enabled="true" pollTime="2000">
....<databases>
......<add name="Demo" connectionStringName="DemoConn" />
....</databases>
..</sqlCacheDependency>
</caching>
3.在Global.asax.cs中以编程的方式启用缓存依赖,如下:
protected void Application_Start()
{
..string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["DemoConn"].ConnectionString;
..System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(connStr);
..System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(connStr, "UserInfo");
}
4.使用命令行注册依赖,如下:
aspnet_regsql -S .\sqlexpress -E -d DBName -ed
aspnet_regsql -S .\sqlexpress -E -d DBName -t TableName -et
示例如下:
[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

}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.NET无限级分类的非递归实现(存储过程版)发布时间:2022-07-10
下一篇:
如何发布一个ASP.NET网站发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap