• MVC过滤器


    MVC框架支持四种不同类型的过滤器,每一种类型允许你再请求处理管道的不同点上引入逻辑。
    这四种过滤器类型描述于下表:
                                MVC框架的过滤器类型

    过滤器类型

    接口

    默认实现

    描述

    Authorization

    I AuthorizationFilter

    AuthorizeAttribute

    最先运行,在任何其他过滤器和动作方法之前

    Action

    IActionFilter

    ActionFilterAttribute

    在动作方法之前及之后运行

    Result

    IResultFilter

    ActionFilterAttribute

    在动作结果被执行之前和之后运行

    Exception

    IExceptionFilter

    HandlerErrorAttribute

    仅在另一个过滤器、动作方法或动作结果抛出异常时运行

    在MVC调用一个动作之前,它首先检测该方法的定义,以查看它是否具有实现上面表中所列接口的注解属性,如果有,那么在请求管道的相应点上调用这些接口所定义的方法。

    过滤器既可被应用于动作方法,也可以运用于整个控制器。

    (自己理解:过滤器的作用相当于ASP.NET中的基类,如BasePage,用于处理共同的逻辑)
    如果为控制器定义了一个自定义基类,那么,运用于这个基类上的任何过滤器都会影响其派生类。

    1、使用授权过滤器
    授权过滤器是首先运行的过滤器,它运行在其他过滤器之前以及动作方法被调用之前。
    创建授权过滤器最简单的办法是创建AuthorizeAttribute类的子类,并重写其AuthorizeCore方法,这确保开发者能够利用内建的AuthorizeAttribute。
    如:

     

    应用:

     
    2、使用异常过滤器
    这种异常来自:
    ◆另一个过滤器(授权、动作、结果过滤器)
    ◆动作方法本身
    ◆当动作结果被执行时
    (备注:异常处理器可用于自定义错误处理)

    3、使用动作和结果过滤器
    动作和结果过滤器是可以被用于任何目的的多用途过滤器。
    OnActionExecuting

    OnActionExecuted

    下面是具体项目中使用动作过滤器的示例(不太规范):
    using SFast;
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Script.Serialization;
    using XFK.JDTrain.Common;
    using XinfuMall.XinfuWeb.PublicClass;
    
    namespace XFK.JDTrain.Web.Controllers
    {
        public class BaseController : Controller
        {
            //
            // GET: /Base/
            public JavaScriptSerializer js = new JavaScriptSerializer();
            public LogHelper log = null;
            public UserSession user { get; set; }
            public EnterpriseAccountSession enterprise { get; set; }
    
            /// <summary>
            /// 执行控制器中方法之前执行的方法
            /// </summary>
            /// <param name="filterContext"></param>
            protected override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                LogHelper.WriteLog("进入BaseController");
                TempData["xinfuUrl"] = System.Configuration.ConfigurationManager.AppSettings["xinfuUrl"];
                if (XFK.Infrastructure.Util.SessionManager.Read(UserUtil.user_sessionStr) != null)
                {
                    user = XFK.Infrastructure.Util.SessionManager.Read(UserUtil.user_sessionStr) as UserSession;
                    object enterpriseId = XFK.Infrastructure.Util.SessionManager.Read(EnterpriseUtil.enterprise_sessionId);
                    enterprise = new EnterpriseAccountSession();
                    enterprise.Id = Convert.ToInt32(enterpriseId);
                    ViewBag.UserName = user.AccountName;
                    ViewBag.Enterpriseid = Convert.ToInt32(enterpriseId);
                    var limitEnterprise = String.IsNullOrEmpty(ConfigurationManager.AppSettings["limitEnterprise"]) ? "" : ConfigurationManager.AppSettings["limitEnterprise"];
                    if (limitEnterprise.Split(',').Contains(enterprise.Id.ToString()))
                    {
                        System.Web.HttpContext.Current.Response._WriteEnd("对不起,所属的企业没有订购此产品的权限!");
                    }
                }
                else
                {
                    filterContext.Result = new RedirectResult(ConfigurationManager.AppSettings["xinfuUrl"] + "mall/Login?originUrl=" + MyUrl.UrlEncode(Request.Url.ToString()));
                    LogHelper.WriteLog("当前访问的路径是:" + ConfigurationManager.AppSettings["xinfuUrl"] + "mall/Login?originUrl=" + MyUrl.UrlEncode(Request.Url.ToString()));
                }
    
                base.OnActionExecuting(filterContext);
            }
        }
    }
    
    本文内容参考:《精通ASP.NET MVC3框架》第13章
  • 相关阅读:
    jpa和postgres实现保存json格式数据
    Linux系统编程03动态库
    Linux系统编程04Makefile
    Linux系统编程09stat和lstat
    Linux系统编程01GCC
    Linux系统编程07read和write
    Linux系统编程05GDB调试
    Linux系统编程08lseek.md
    C++面向对象构造函数与析构函数
    Linux系统编程06open
  • 原文地址:https://www.cnblogs.com/zhaow/p/9753913.html
Copyright © 2020-2023  润新知