• 过滤器


    1.在Models中添加控制器类

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using System.Web;
     7 using System.Web.Mvc;
     8 
     9 namespace MvcValidateDemo.Models
    10 {
    11     //AllowMultiple =true允许多个标签同时起作用
    12     //[AttributeUsage(AttributeTargets.All,AllowMultiple =true)]
    13    public  class MyActionFilterAttribute:ActionFilterAttribute
    14     {
    15         //Action执行之前执行方法
    16        public string Name { get; set; }
    17         public override void OnActionExecuting(ActionExecutingContext filterContext)
    18         {
    19             base.OnActionExecuting(filterContext);
    20             //HttpContext.Current.Session[""];
    21             HttpContext.Current.Response.Write("<br/>OnActionExecuting"+Name);
    22         }
    23         //Action执行之后
    24         public override void OnActionExecuted(ActionExecutedContext filterContext)
    25         {
    26             base.OnActionExecuted(filterContext);
    27             HttpContext.Current.Response.Write("<br/>OnActionExecuted" + Name);
    28         }
    29         //ActionResult执行之前执行此方法
    30         public override void OnResultExecuting(ResultExecutingContext filterContext)
    31         {
    32             base.OnResultExecuting(filterContext);
    33             HttpContext.Current.Response.Write("<br/>OnReslutExecuting" + Name);
    34         }
    35         //ActionResult执行之后
    36         public override void OnResultExecuted(ResultExecutedContext filterContext)
    37         {
    38             base.OnResultExecuted(filterContext);
    39             HttpContext.Current.Response.Write("<br/>OnReslutExecuted" + Name);
    40         }
    41     }
    42 }

    2.添加Home控制器

     1 using MvcValidateDemo.Models;
     2 using System;
     3 using System.Collections.Generic;
     4 using System.Linq;
     5 using System.Web;
     6 using System.Web.Mvc;
     7 
     8 namespace MvcValidateDemo.Controllers
     9 {
    10     [MyActionFilter(Name = "Home Action")]
    11     public class HomeController : Controller
    12     {
    13         
    14         // GET: Home
    15         [MyActionFilter(Name = "Index Action")]
    16         public ActionResult Index()
    17         {
    18             Response.Write("<p>Action执行了</p>");
    19             return Content("OK,视图被渲染");
    20         }
    21         //[MyActionFilter(Name="About  Action")]
    22         public ActionResult About()
    23         {
    24             //throw new Exception("demo");
    25             return Content("<p>About 渲染</p>");
    26         }
    27     }
    28 }

    全局异常过滤器

    1.在Models中添加MyExceptionFilterAttribute控制器类

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.Mvc;
     6 
     7 namespace _111111.Models
     8 {
     9     public class MyEexeptionFilterAttribute : HandleErrorAttribute
    10     {
    11         public override void OnException(ExceptionContext filterContext)
    12         {
    13             base.OnException(filterContext);
    14 
    15             HttpContext.Current.Response.Redirect("/Home/Index");
    16         }
    17     }
    18 }

    在Home/About中手动构建一个异常

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.Mvc;
     6 
     7 namespace _111111.Controllers
     8 {
     9     public class HomeController : Controller
    10     {
    11         //
    12         // GET: /Home/
    13 
    14         public ActionResult Index()
    15         {
    16             return Content("dsjfjsj");
    17         }
    18         public ActionResult About()
    19         {
    20             throw new Exception("demo");
    21         }
    22 
    23     }
    24 }

    将自己的异常过滤器注册到全局过滤器中

     1 using _111111.Models;
     2 using System.Web;
     3 using System.Web.Mvc;
     4 
     5 namespace _111111
     6 {
     7     public class FilterConfig
     8     {
     9         public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    10         {
    11             filters.Add(new MyEexeptionFilterAttribute());
    12         }
    13     }
    14 }
  • 相关阅读:
    6.数字三角形
    5.分组背包问题
    1.商品模块表结构分析
    AUTH_USER_MODEL refers to model 'user.User' that has not been installed
    发布品论接口
    查询指定课程评论接口
    1.评论模块表结构
    上传视频课程到七牛云后存储到django后端接口
    5.上传视频到七牛云django端实现
    4.七牛云上传前台页面
  • 原文地址:https://www.cnblogs.com/qinsilandiao/p/5031359.html
Copyright © 2020-2023  润新知