• Asp.Net Mvc Filter


    著作权归作者所有。
    商业转载请联系作者获得授权,非商业转载请注明出处。
    作者:卷猫
    链接:http://anneke.cn/ArticleInfo/Detial/19
    来源:Anneke.cn
    
    public class OwnActionFilter : ActionFilterAttribute,IAuthorizationFilter,IExceptionFilter
        {
            /// <summary>
            /// 最先执行
            /// </summary>
            /// <param name="filterContext"></param>
            public void OnAuthorization(AuthorizationContext filterContext)
            {
                filterContext.HttpContext.Response.Write("方法名称:OnAuthorization<br/>");
                //输出请求的控制器与action名称
                String controllerName = filterContext.RouteData.Values["controller"].ToString();
                String actionName = filterContext.RouteData.Values["action"].ToString();
                String message = String.Format("控制器:{0}<br/>action:{1}<br/>", controllerName, actionName);
                filterContext.HttpContext.Response.Write(message);
                filterContext.HttpContext.Response.Write("------------------华丽的分割线------------------<br/>");
            }
     
            /// <summary>
            /// 在action method之前执行
            /// </summary>
            /// <param name="filterContext"></param>
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                filterContext.HttpContext.Response.Write("方法名称:OnActionExecuting<br/>");
                //输出请求的控制器与action名称
                String controllerName = filterContext.RouteData.Values["controller"].ToString();
                String actionName = filterContext.RouteData.Values["action"].ToString();
                String message = String.Format("控制器:{0}<br/>action:{1}<br/>", controllerName, actionName);
                filterContext.HttpContext.Response.Write(message);
                filterContext.HttpContext.Response.Write("------------------华丽的分割线------------------<br>");
            }
            /// <summary>
            /// 在action method之后执行
            /// </summary>
            /// <param name="filterContext"></param>
            public override void OnActionExecuted(ActionExecutedContext filterContext)
            {
                filterContext.HttpContext.Response.Write("方法名称:OnActionExecuted<br/>");
                //输出请求的控制器与action名称
                String controllerName = filterContext.RouteData.Values["controller"].ToString();
                String actionName = filterContext.RouteData.Values["action"].ToString();
                String message = String.Format("控制器:{0}<br/>action:{1}<br/>", controllerName, actionName);
                filterContext.HttpContext.Response.Write(message);
                filterContext.HttpContext.Response.Write("------------------华丽的分割线------------------<br/>");
            }
     
            /// <summary>
            /// 在返回view之前执行
            /// </summary>
            /// <param name="filterContext"></param>
            public override void OnResultExecuting(ResultExecutingContext filterContext)
            {
                filterContext.HttpContext.Response.Write("方法名称:OnResultExecuting<br/>");
                //输出请求的控制器与action名称
                String controllerName = filterContext.RouteData.Values["controller"].ToString();
                String actionName = filterContext.RouteData.Values["action"].ToString();
                String message = String.Format("控制器:{0}<br/>action:{1}<br/>", controllerName, actionName);
                filterContext.HttpContext.Response.Write(message);
                filterContext.HttpContext.Response.Write("------------------华丽的分割线------------------<br/>");
            }
     
            /// <summary>
            /// 在返回View之后执行
            /// </summary>
            /// <param name="filterContext"></param>
            public override void OnResultExecuted(ResultExecutedContext filterContext)
            {
                filterContext.HttpContext.Response.Write("方法名称:OnResultExecuted<br/>");
                //输出请求的控制器与action名称
                String controllerName = filterContext.RouteData.Values["controller"].ToString();
                String actionName = filterContext.RouteData.Values["action"].ToString();
                String message = String.Format("控制器:{0}<br/>action:{1}<br/>", controllerName, actionName);
                filterContext.HttpContext.Response.Write(message);
                filterContext.HttpContext.Response.Write("------------------华丽的分割线------------------<br/>");
            }
     
          
            /// <summary>
            /// 当出现异常时才执行
            /// </summary>
            /// <param name="filterContext"></param>
            public void OnException(ExceptionContext filterContext)
            {
                filterContext.HttpContext.Response.Write("方法名称:OnException<br/>");
                //输出请求的控制器与action名称
                String controllerName = filterContext.RouteData.Values["controller"].ToString();
                String actionName = filterContext.RouteData.Values["action"].ToString();
                String message = String.Format("控制器:{0}<br/>action:{1}", controllerName, actionName);
                filterContext.HttpContext.Response.Write(message);
                filterContext.HttpContext.Response.Write("------------------华丽的分割线------------------<br/>");
            }
        }

    执行效果:

  • 相关阅读:
    java.lang.IllegalStateException: Cannot get a STRING value from a NUMERIC cell
    spring boot 启动报:Composite-id class must implement Serializable: xxx错误
    IntelliJ IDEA 中使用region代码折叠
    JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String
    MySQL生成UUID
    PowerDesigner设计表时显示注释列Comment,Columns中没有Comment的解决办法
    使用PowerDesigner创建物理模型并生成SQL语句在MySQL中生成表
    IMP-00003: 遇到 ORACLE 错误 959 ORA-00959: 表空间 '' 不存在
    SP2-0734: 未知的命令开头 “IMP ”
    使用imp命令和exp命令对oracle数据库进行导入导出操作
  • 原文地址:https://www.cnblogs.com/zzqvq/p/5855445.html
Copyright © 2020-2023  润新知