Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

363
Visualizações
Redirect to page OnActionExecuting method ASP.NET Core 5 MVC

I have an issue that the request redirected too many times and I just want to go to new redirect route, so how can I prevent that or is there something that I am not aware of?

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" }
            });
        }
    }
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório

0

Try this:

filterContext.Result = new RedirectToActionResult ("<Action>", "<Controller>", null);
base.OnActionExecuting(filterContext);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda