mvc filter 跳转页面 无法在发送 HTTP 标头之后进行重定向(转)
public class LoginFilterAttribute : ActionFilterAttribute { //表示是否检查登录 public bool IsCheck { get; set; } //Action方法执行之前执行此方法 public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); if (IsCheck) { var returnUrl = filterContext.HttpContext.Request["ReturnUrl"]; if (string.IsNullOrEmpty(returnUrl)) returnUrl = filterContext.HttpContext.Request.Url.ToString(); //校验用户是否已经登录 if (!LoginHelper.Instance.IsLogin) { //跳转到登陆页 if (filterContext.HttpContext.Request.IsAjaxRequest()) { filterContext.Result = new JsonResult { Data = new { ajaxValidLogin = false, message = "登录已失效,请重新登录", ReturnUrl = returnUrl } }; } else { filterContext.Result = new EmptyResult();////加入EmptyResult就告诉ASP.NET MVC在本拦截器执行结束后,不必再为当前请求执行Controller中Action的代码 filterContext.HttpContext.Response.Redirect("/Account/Login?ReturnUrl=" + HttpUtility.UrlEncode(returnUrl),true); } } } } }
对修改封闭,向扩展开放,我反而更向往自由