-
判断登录与否或用户权限
决策输出缓存
防盗链
防蜘蛛
本地化与国际化设置
实现动态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();
}