码迷,mamicode.com
首页 > Web开发 > 详细

MVC Filter中加入验证并跳转

时间:2014-12-18 11:46:54      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   ar   io   color   使用   sp   for   

    public class BuildingFilter : ActionFilterAttribute
    {
        /// <summary>
        /// 验证 Building Cookie add xia 验证是否登陆,是否选择案场
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var cookie = filterContext.HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName];
            if (cookie == null)
            {
                filterContext.Result = new RedirectResult("/Login/LoginOut");
                return;
            }
            var buildingCookie = filterContext.HttpContext.Request.Cookies["Building"];
            if (buildingCookie == null)
            {
                filterContext.Result = new RedirectResult("/BuildingList/Index");
                return;
            }
            else
            {
                long buildingSysNo;
                long relationSysNo;
                //关系SysNo
                long.TryParse(buildingCookie.Values["RelationSysNo"], out relationSysNo);
                //案场SysNo
                long.TryParse(buildingCookie.Values["BuildingSysNo"], out buildingSysNo);
                if (buildingSysNo == 0 || relationSysNo == 0)
                {
                    filterContext.Result = new RedirectResult("/BuildingList/Index");
                    return;
                }
            }
            base.OnActionExecuting(filterContext);
        }
    }

我是验证是否登陆和是否选择案场【一并验证】

使用方法如下:

        /// <summary>
        /// 签约列表 -GET
        /// </summary>
        /// <returns></returns>
        [BuildingFilter]
        public ActionResult ContractOrderList()
        {
            return View();
        }

 

MVC Filter中加入验证并跳转

标签:style   blog   http   ar   io   color   使用   sp   for   

原文地址:http://www.cnblogs.com/onlyinweb/p/4171134.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!