狼一匹

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

MVC3 Remote 远程验证排除特定action(添加与编辑验证不同)的问题

/// <summary>
        /// 验证角色名称唯一
        /// </summary>
        /// <param name="name"></param>
        /// <returns>true验证通过</returns>
        [HttpGet]
        public JsonResult CheckName(string name)
        {
            string url = Request.UrlReferrer.OriginalString;
            HttpRequest hr = new HttpRequest("", url, "");
            HttpResponse hrs = new HttpResponse(new StringWriter());
            HttpContext hc = new HttpContext(hr, hrs);
            HttpContextWrapper hcw = new HttpContextWrapper(hc);
            RouteData rd = RouteData.Route.GetRouteData(hcw);
            string controllerName = rd.Values["controller"].ToString().ToLower();
            string actionName = rd.Values["action"].ToString().ToLower();
            string id = rd.Values["id"].ToString().ToLower();;
            int actionID = string.IsNullOrEmpty(id) ? 0 : int.Parse(id);
            bool result = true;
            var role = _roleService.GetRole(name);
            if (role != null)
            {
                if (actionID == 0)
                    result = false;//  添加 
                else
                    result = !_roleService.Entities.Any(r => r.Name.Equals(name) && r.ID != actionID);
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }
posted on 2012-06-27 16:46  狼一匹  阅读(168)  评论(0)    收藏  举报