1.controller
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace TestMvc.Controllers { public class HomeController : Controller { [CustomFilter] [ResultFilter] public ActionResult Index() { ViewBag.Title = "Home Page"; //throw new Exception(""); return View(); } } }
2.filters
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web.Mvc; namespace TestMvc { public class CustomFilter : ActionFilterAttribute { public override void OnActionExecuted(ActionExecutedContext filterContext) { base.OnActionExecuted(filterContext); } public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); } } public class ExFilter : IExceptionFilter { public void OnException(ExceptionContext filterContext) { // throw new NotImplementedException(); } } public class ResultFilter : ActionFilterAttribute,IResultFilter { public override void OnResultExecuted(ResultExecutedContext filterContext) { throw new NotImplementedException(); } public override void OnResultExecuting(ResultExecutingContext filterContext) { throw new NotImplementedException(); } } }
3.global filter
using System.Web; using System.Web.Mvc; namespace TestMvc { public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new ExFilter()); filters.Add(new HandleErrorAttribute()); } } }
时间: 2024-10-11 01:47:54