ASP.NET MVC 第六回 过滤器Filter
判断登录与否或用户权限
决策输出缓存
防盗链
防蜘蛛
本地化与国际化设置
实现动态Action
Filter要继承于ActionFilterAttribute抽象类,并可以覆写void OnActionExecuting(ActionExecutingContext)和
void OnActionExecuted(ActionExecutedContext)
void OnResultExecuted(ResultExecutedContext)
一、应用于Action的Filter
下面我给大家一个示例,来看看它的的执行顺序
首先我们先建立 一个Filter,名字叫做TestFilter
public class TestFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.HttpContext.Session["temp"] += "TestFilter OnActionExecuting<br/>";
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.HttpContext.Session["temp"] += "TestFilter OnActionExecuted<br/>";
}
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.HttpContext.Session["temp"] += "TestFilter OnResultExecuting<br/>";
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
filterContext.HttpContext.Session["temp"] += "TestFilter OnResultExecuted<br/>";
}
}
[TestFilter]//将此Filter应用于Action
public ActionResult filteraction()
{
return View();
}
<%Session["temp"] += "View Execute<br/>"; %>
TestFilter OnActionExecuting
TestFilter OnActionExecuted
TestFilter OnResultExecuting
View Execute
TestFilter OnResultExecuted
二、Controller的Filter
[TestFilter]
public class EiceController : Controller
{
}
OnActionExecuting/OnActionExecuted/OnResultExecuting/OnResultExecuted的四个方法。
三、AcceptVerbs
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Example(){
return View();
}
页面只能以Post形式访问,即表单提交。
四、ActionName
[ActionName("class")]
public ActionResult Example(){
return View();
}
五、NonAction
六、OutputCache
[OutputCache(Duration = 60, VaryByParam = "*")]
public ActionResult Example()
{
return View();
}
七、ValidateInput
[ValidateInput(false)]
public ActionResult Example()
{
return View();
}
八、ValidateAntiForgeryTokenAttribute
[ValidateAntiForgeryToken]
public ActionResult Example()
{
return View();
}
本文出自 “典之博客” 博客,请务必保留此出处http://chsword.blog.51cto.com/666596/137929
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。