Example #1
0
        //加上如下的重写方法,在方法内进行跳转,问题解决
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            if (_IsNoPermissionToAccess)
            {
                filterContext.Result = new JsonResult()
                {
                    Data                = ResponseModel <string> .Invalid(MessageHelper.NoPermissionToAccess),
                    ContentEncoding     = System.Text.Encoding.UTF8,
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
                return;
            }

            string url = $"{_LoginUri}?returnURL=";

            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                url += HttpUtility.UrlEncode(filterContext.HttpContext.Request.UrlReferrer.ToString());
                filterContext.Result = new JsonResult()
                {
                    Data                = ResponseModel <string> .Logout(url, MessageHelper.NoSession),
                    ContentEncoding     = System.Text.Encoding.UTF8,
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }
            else
            {
                url += HttpUtility.UrlEncode(filterContext.HttpContext.Request.Url.ToString());
                filterContext.Result = new RedirectResult(url);
            }
        }
        protected override void HandleUnauthorizedRequest(HttpActionContext actionContext)
        {
            if (_IsNoPermissionToAccess)
            {
                var response = actionContext.Response = actionContext.Response ?? new HttpResponseMessage();
                response.StatusCode = HttpStatusCode.OK;
                ResponseModel <string> responseModel = ResponseModel.Invalid(MessageHelper.NoPermissionToAccess);
                response.Content = new StringContent(SerializationHelper.ToJson(responseModel), Encoding.UTF8, "application/json");
                return;
            }

            base.HandleUnauthorizedRequest(actionContext);
        }
        public override void OnActionExecuting(ActionExecutingContext actionContext)
        {
            var viewData   = actionContext.Controller.ViewData;
            var modelState = viewData.ModelState;

            if (!modelState.IsValid)
            {
#if DEBUG
                StringBuilder sbError = new StringBuilder();
                sbError.AppendLine("<p>Please note the errors below:</p>");
                foreach (var key in modelState.Keys)
                {
                    var state = modelState[key];
                    if (state.Errors.Any())
                    {
                        sbError.AppendLine($"<p>{state.Errors.First().ErrorMessage}</p>");
                    }
                }

                actionContext.Result = new JsonResult
                {
                    Data                = ResponseModel <string> .Invalid(sbError.ToString()),
                    ContentEncoding     = Encoding.UTF8,
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
#else
                actionContext.Result = new JsonResult
                {
                    //Data = ResponseModel<string>.Invalid<string>(sbError.ToString()),
                    Data                = ResponseModel <string> .Invalid("请先填写有效的数据。"),
                    ContentEncoding     = System.Text.Encoding.UTF8,
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
#endif
            }
        }
 protected ActionResult Invalid(string strMsg)
 {
     return(Return(ResponseModel <string> .Invalid(strMsg)));
 }
Example #5
0
 protected IHttpActionResult Invalid(string strMsg)
 {
     return(Return(ResponseModel.Invalid(strMsg)));
 }