• ASP.NET MVC 拦截器


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web.Mvc;
    
    
    namespace Rhythmk.Sercurity.Attribute
    {
        [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)]
       public  class MeAttribute : FilterAttribute, IAuthorizationFilter, IActionFilter
        {
            //IAuthorizationFilter
            public void OnAuthorization(AuthorizationContext filterContext)
            {
                filterContext.RequestContext.HttpContext.Response.Write("OnAuthorization<br/>");
            }
    
            //IActionFilter
            public void OnActionExecuted(ActionExecutedContext filterContext)
            {
                filterContext.RequestContext.HttpContext.Response.Write("OnActionExecuted<br/>");
            }
    
            //IActionFilter
            public void OnActionExecuting(ActionExecutingContext filterContext)
            {
                filterContext.Controller.ViewBag.CurrentData = "http://Rhythmk.cnblogs.com";
                if (filterContext.ActionParameters.ContainsKey("user"))
                {
                    filterContext.ActionParameters["user"] = "UserID=rhythmk";
                }
    
                filterContext.RequestContext.HttpContext.Response.Write("OnActionExecuting<br/>");
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Security;
    using Rhythmk.Sercurity.Attribute;
    
    namespace MvcApp2.Controllers
    {
        public class HomeController : Controller
        {
            [Me]  // http://rhythmk.cnblogs.com
            public ActionResult Index(string user)
            {
                HttpContext.Response.Write(string.Format("ViewBag.CurrentData:{0}<br/>", ViewBag.CurrentData));
                HttpContext.Response.Write("Home/Index<br/>");
    
                HttpContext.Response.Write(string.Format("user:{0}<br/>",user));
    
                return View();
            }
    
           
        }
    }

    输出结果:

    OnAuthorization
    OnActionExecuting
    ViewBag.CurrentData:http://Rhythmk.cnblogs.com
    Home/Index
    user:UserID=rhythmk
    OnActionExecuted

    异常拦截:

    public class ActionErrorAttribute : ActionFilterAttribute, IExceptionFilter
        {
            /// <summary>
            /// 过滤异常信息
            /// </summary>
            /// <param name="filterContext"></param>
            public void OnException(ExceptionContext filterContext)
            {
                Exception error = filterContext.Exception;

               
                filterContext.ExceptionHandled = true; //设置异常已经处理
                filterContext.RequestContext.HttpContext.Response.Write(error.Message);
            }
        }

    使用:

     [ActionError]
     public ActionResult TestError()
     {
         int i = int.Parse("rhythmk");
        return Content("OK");
     }

        通过拦截器 传入参数写操作日志:

    public void OnActionExecuted(ActionExecutedContext filterContext)
    {
      string routeId = string.Empty;
          if (filterContext.Controller.ViewData["CompanyId"] != null)
          {
             routeId = filterContext.Controller.ViewData["CompanyId"].ToString();
           }
      filterContext.RequestContext.HttpContext.Response.Write("OnActionExecuted<br/>"+routeId);
    }

        

  • 相关阅读:
    BroadcastReceiver总结
    一二三(The Seventh Hunan Collegiate Programming Contest)
    使用 JQueryMobile 点击超链接提示“error loading page” 错误
    盒子游戏(The Seventh Hunan Collegiate Programming Contest)
    遗留系统升级改造方案思路
    根据外接鼠标控制笔记本触摸板禁用或启用
    设计模式之策略模式
    android4.3环境搭建
    清晰明亮的白色lua协程(coroutine)
    基于JUnit和Ant测试程序正在运行使用Kieker(AspectJ)监测方法
  • 原文地址:https://www.cnblogs.com/rhythmK/p/2855472.html
Copyright © 2020-2023  润新知