C# MVC4 执行特性之后不再执行Action

it2022-05-09  30

关键地方, 在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,转到登录页面。

 

转载于:https://www.cnblogs.com/xygui/p/5584033.html

相关资源:数据结构—成绩单生成器

最新回复(0)