Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

370
Views
Redirigir a la página OnActionExecuting method ASP.NET Core 5 MVC

Tengo un problema en el que la solicitud se redirigió demasiadas veces y solo quiero ir a una nueva ruta de redirección, entonces, ¿cómo puedo evitar eso o hay algo de lo que no estoy al tanto?

 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 answers
Answer question

0

El bucle de redirección es bastante claro. Su solicitud redirigida debe ser identificable para que su código pueda verificar y no realizar la redirección para esa solicitud redirigida (por lo que no se ejecutará ningún bucle y causará el error de demasiados redireccionamientos).

Tu código puede ser tan simple como esto:

 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" } }); } }

NOTA : el GoToLogin en su código no está claro, si quiere decir que no sabe qué condición para él (para evitar el bucle de redirección), simplemente configúrelo así:

 var goToLogin = !Equals(controller.TempData["__redirected"], true);

O para no mutar su valor:

 if (GoToLogin && !Equals(controller.TempData["__redirected"], true)){ //redirect the request ... }

El beneficio de usar TempData aquí es que se elimina automáticamente después de leerlo por primera vez. Entonces, en el momento de recibir la solicitud redirigida, el valor contenido en TempData es true , lo que hace que todo el GoToLogin false (o no cumple la condición para redirigir) y la redirección no se realizará. Después de eso, el valor contenido en TempData se borra (elimina) y está listo para la próxima redirección.

over 4 years ago · Santiago Trujillo Report

0

Prueba esto:

 filterContext.Result = new RedirectToActionResult ("<Action>", "<Controller>", null); base.OnActionExecuting(filterContext);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!