MVC 行为过滤器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace FilterExam.Controllers
{
public class HelloController : Controller
{
[MyAction]
[MyResult]
// GET: Hello
public ActionResult Index()
{
Response.Write("行为执行中<br>");
return View();
}
}
}
//行为过滤器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace FilterExam.Fiter
{
public class MyAction:ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.HttpContext.Response.Write("ing<br>");
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.HttpContext.Response.Write("ed<br>");
}
}
}
//结果过滤器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace FilterExam.Fiter
{
public class MyResult:ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.HttpContext.Response.Write("结果执行前<br>");
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
filterContext.HttpContext.Response.Write("结果执行后<br>");
}
}
}
ing
行为执行中
ed
结果执行前