关键地方, 在OnActionExecuting函数中,不能使用filterContext.HttpContext.Response.Redirect(url)来定向,打断点可以发现一样会执行action里面代码。应该设置filterContext.Result来控制转向。
1 string returnUrl = HttpContext.Current.Request.Url.PathAndQuery; 2 var url = FormsAuthentication.LoginUrl + "?returnUrl=" + HttpUtility.UrlEncode(returnUrl); 3 //HttpContext.Current.Response.Redirect(url, true);//进入action 4 //filterContext.HttpContext.Response.Redirect(url);//进入action 5 /*filterContext.Result = new RedirectToRouteResult( //不进入action 6 new RouteValueDictionary 7 { 8 { "action", "Login" }, 9 { "controller", "Admin" }, 10 {"returnUrl", returnUrl} 11 });*/ 12 filterContext.Result = new RedirectResult(url);//不进入action,转到登录页面。
时间: 2024-10-27 06:41:22