重定向到页面 OnActionExecuting 方法 ASP.NET Core 5 MVC
Redirect to page OnActionExecuting method ASP.NET Core 5 MVC
我有一个请求重定向次数太多的问题,我只想转到新的重定向路由,那么我该如何防止这种情况发生或者有什么我不知道的地方?
public class AuthController : Controller
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
if (GoToLogin)
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary
{
{ "controller", "account" },
{ "action", "login" }
});
}
}
}
试试这个:
filterContext.Result = new RedirectToActionResult ("<Action>", "<Controller>", null);
base.OnActionExecuting(filterContext);
重定向的循环很清楚。您的重定向请求必须是可识别的,以便您的代码可以检查并且不会为该重定向请求执行重定向(因此不会执行循环并导致太多重定向错误)。
你的代码可以像这样简单:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
//obtain the controller instance
var controller = context.Controller as Controller;
//not sure where you define the GoToLogin here, I assume it's available as a bool
GoToLogin &= !Equals(controller.TempData["__redirected"], true);
if (GoToLogin)
{
//set a temp data value to help identify this kind of redirected request
//which will be later sent by the client.
//The temp data value will be deleted the next time read (the code above)
controller.TempData["__redirected"] = true;
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary
{
{ "controller", "account" },
{ "action", "login" }
});
}
}
注意:你代码中的GoToLogin
不清楚,如果你的意思是你不知道什么条件为此(为了防止重定向循环),只需像这样设置它:
var goToLogin = !Equals(controller.TempData["__redirected"], true);
或者不改变它的值:
if (GoToLogin && !Equals(controller.TempData["__redirected"], true)){
//redirect the request ...
}
这里使用TempData
的好处是第一次阅读后自动删除。所以在收到重定向请求的时候,TempData
中包含的值为true
,使得整个GoToLogin
false
(或者不满足重定向的条件)和不会执行重定向。之后 TempData
中包含的值被清除(删除)并为下一次重定向做好准备。
我有一个请求重定向次数太多的问题,我只想转到新的重定向路由,那么我该如何防止这种情况发生或者有什么我不知道的地方?
public class AuthController : Controller
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
if (GoToLogin)
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary
{
{ "controller", "account" },
{ "action", "login" }
});
}
}
}
试试这个:
filterContext.Result = new RedirectToActionResult ("<Action>", "<Controller>", null);
base.OnActionExecuting(filterContext);
重定向的循环很清楚。您的重定向请求必须是可识别的,以便您的代码可以检查并且不会为该重定向请求执行重定向(因此不会执行循环并导致太多重定向错误)。
你的代码可以像这样简单:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
//obtain the controller instance
var controller = context.Controller as Controller;
//not sure where you define the GoToLogin here, I assume it's available as a bool
GoToLogin &= !Equals(controller.TempData["__redirected"], true);
if (GoToLogin)
{
//set a temp data value to help identify this kind of redirected request
//which will be later sent by the client.
//The temp data value will be deleted the next time read (the code above)
controller.TempData["__redirected"] = true;
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary
{
{ "controller", "account" },
{ "action", "login" }
});
}
}
注意:你代码中的GoToLogin
不清楚,如果你的意思是你不知道什么条件为此(为了防止重定向循环),只需像这样设置它:
var goToLogin = !Equals(controller.TempData["__redirected"], true);
或者不改变它的值:
if (GoToLogin && !Equals(controller.TempData["__redirected"], true)){
//redirect the request ...
}
这里使用TempData
的好处是第一次阅读后自动删除。所以在收到重定向请求的时候,TempData
中包含的值为true
,使得整个GoToLogin
false
(或者不满足重定向的条件)和不会执行重定向。之后 TempData
中包含的值被清除(删除)并为下一次重定向做好准备。