What should I put as condition in OnActionExecuting to avoid an infinite redirect loop?

Viewed 20

I have a global filter:

public class GeoLookupActionFilter : IActionFilter
{    
    public void OnActionExecuting(ActionExecutingContext context)
    {
         if (condition?) // What should I put as condition on this line to avoid an infinite redirect loop?
            return;

         if (context.HttpContext.Request.Cookies["localization"] != null)
         {
             var isoCode = context.HttpContext.Request.Cookies["localization"];

             if (isoCode == "en-us")
             {
                 context.Result = new RedirectResult("https://www.company.com/en-us/home");
             }

             if (isoCode == "es-mx")
             {
                 context.Result = new RedirectResult("https://www.company.com/es-mx/casa");
             }
         }
     }
 }

What should I put as condition on line 5 in OnActionExecuting to avoid an infinite redirect loop?

Edit: The RedirectResult would also probably need to look something like this (because I don't want to redirect to an external URL; I want to redirect to the Index action method of the Home controller):

new RedirectToRouteResult(
                new RouteValueDictionary(new {controller = _controller, action = _action})
                );

and I would probably need to add /en-us/home or /es-mx/case in addition to that somehow.

1 Answers

I think you can get Url referrer uisng Request object and compare it with current Url. If they are same you can bypass other codes

Related