zoukankan      html  css  js  c++  java
  • Action过滤器使用实例(一)

    1.实例一

        /// <summary>
        ///  需要用户登陆的 action,执行提前验证
        /// </summary>
        public class LoginFilterAttribute : FilterAttribute, IActionFilter
        {
            void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
            {
            }
            /// <summary>
            /// 执行action 前验证 用户是否登陆,如果没有登陆返回字符串体质
            /// </summary>
            /// <param name="filterContext"></param>
            void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
            {
                // throw new NotImplementedException();
                //1.判断
                if (A_UserHelper.IsLogin() == false)
                {
                    ContentResult result = new ContentResult();
                    result.Content = "亲,还没有登陆,请先登录";
                    filterContext.Result = result;
                }
            }
    
        }

    2.实例二

        public class LoginGoAttribute : FilterAttribute, IActionFilter
        {
            void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
            {
                //throw new NotImplementedException();
            }
            /// <summary>
            /// 执行action 之前 验证用户是否登陆,如果没有登陆返回登陆页面
            /// </summary>
            /// <param name="filterContext"></param>
            void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
            {
                //throw new NotImplementedException();
                if (A_UserHelper.IsLogin() == false)
                {
                    //不跳转在当前地址返回登陆页------
                    //RouteData route = filterContext.RouteData;
                    //route.Values["controller"] = "user";
                    //route.Values["action"] = "login";
                    //IController IController = new UserController();
                    //HttpContextBase context = filterContext.HttpContext;
                    //IController.Execute(new RequestContext(context, route));
                    //filterContext.Result = new ContentResult();
    
    
    
    
                    //重定向到登陆页--------不推荐使用,因为当前action还是会执行
                    //HttpContextBase context = filterContext.HttpContext;
                    //context.Response.Redirect(SiteUrl.GetLogin(),true);
                    //context.Response.End();
                    //filterContext.ActionDescriptor = null;
    
    
                    //临时重定向到登陆页
                    HttpRequestBase req = filterContext.HttpContext.Request;
                    string url = SiteUrl.GetLogin() + "?redirecturl=" + req.Url.AbsoluteUri;
                    RedirectResult result = new RedirectResult(url, false);
                    filterContext.Result = result;
    
    
    
                    //此方法 失败
                    //HttpContextBase context = filterContext.HttpContext;
                    //UserController userC = new UserController();
                    //userC.ControllerContext = new ControllerContext();
                    //userC.ControllerContext.RouteData.DataTokens["controller"] = "user";
                    //userC.ControllerContext.RouteData.Values["area"] = "";
                    //ContentResult content = new ContentResult();
                    //content.Content = ViewHelper.RenderViewToString(userC, "login", null); ;
                    //filterContext.Result = content;
    
                }
            }
        }

    更多:

    MVC4过滤器:http://www.cnblogs.com/tianma3798/p/4666544.html

  • 相关阅读:
    3139:[HNOI2013]比赛
    3143: [Hnoi2013]游走
    目前游戏行业内部主要几款游戏引擎的技术对比
    6.使用AngularJS模板来创建视图
    css选择器(E[att^=”val”]序号选择器)
    5.把作用域实现为数据模型
    4.了解AngularJS模块和依赖注入
    3.创建基本的AngularJS应用
    2.AngularJS MVC
    1.AngularJS初探
  • 原文地址:https://www.cnblogs.com/tianma3798/p/4666981.html
Copyright © 2011-2022 走看看