How to redirect from OnActionExecuting in Base Controller?
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
...
if (needToRedirect)
{
...
filterContext.Result = new RedirectResult(url);
return;
}
...
}
Redirect to page OnActionExecuting method ASP.NET Core 5 MVC
The loop of redirecting is fairly clear. Your redirected request must be identifiable so that your code can check and does not perform the redirection for that redirected request (so no loop will be executed and cause the too-many-redirect error).
Your code can be just simple like this:
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" }
});
}
}
NOTE: the GoToLogin
in your code is not clear, if you mean you don't know what condition for it (to prevent the loop of redirection), just set it like this:
var goToLogin = !Equals(controller.TempData["__redirected"], true);
Or to not mutate its value:
if (GoToLogin && !Equals(controller.TempData["__redirected"], true)){
//redirect the request ...
}
The benefit of using TempData
here is to make it auto-deleted after reading the first time. So at the time receiving back the redirected request, the value contained in TempData
is true
, making the whole GoToLogin
false
(or not met the condition to redirect) and the redirection will not be performed. After that the value contained in the TempData
is cleared (deleted) and ready for the next time of redirection.
How to redirect in a base controller's OnActionExecuting method
You shouldn't call ExecuteResult() from within an action filter. Setting filterContext.Result to a non-null value is sufficient to prevent the MVC pipeline from invoking further filters.
The subclassed type should instead be changed to read:
base.OnActionExecuting(filterContext);
if (filterContext.Result != null) {
return; // something got short-circuited
}
Redirect in OnActionExecuting not working
You can't use RedirectToAction
inside filters. RedirectToAction
is a member of the Controller
class. That's why you can call this method inside action methods which are the members of the class which inherits from Controller
class.
But, if we are talking about filters, then you must set filterContext.Result
to a new RedirectToRouteResult
:
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(
new
{
controller = "Users",
action = "Login"
}));
Url redirection on OnActionExecuting method
Try RedirectResult
filterContext.Result = new RedirectResult("https://website.com");
Related Topics
Entity Framework Filter "Expression<Func<T, Bool>>"
Why \B Does Not Match Word Using .Net Regex
C# Datagridview Checkbox Checked Event
Crud Operations Using Datagridview, Datatable and Dataadapter - Cannot Add New Row to Datagridview
Error Making Azure Management Library API Call When Authenticating with Azure Active Directory
Image Is Not Drawn at the Correct Spot
Is Unityscript/JavaScript Discontinued
How to Convert Hex to a Byte Array
Thread Parameters Being Changed
How to Ignore Ssl_Client_Socket_Impl.Cc(1061)] Handshake Failed in Selenium C# Chromedriver
How to Make Linq Execute a (Sql) Like Range Search
An Error Occurred During Report Processing. -Rldc Reporting in ASP.NET MVC
Sending an Array of Values to Oracle Procedure to Use in Where in Clause
Refactoring Code to Avoid Anti-Pattern
How to Force My C# Winforms Program Run as Administrator on Any Computer