MVC 5 Redirect the user to a page based on CultureInfo

22 Views Asked by At

My application is using ASP.NET MVC 5. I am trying to redirect the user to a page based on their CultureInfo using the following code:

public ActionResult Index()
{
    Response.Clear();
    var userLanguages = Request.UserLanguages;
    CultureInfo ci = null;

    if (userLanguages.Any())
    {
        try
        {
            if (userLanguages != null)
            {
                ci = new CultureInfo(userLanguages[0]);
                ViewBag.Lang = ci.TwoLetterISOLanguageName;
                ViewBag.Message = "English";

                if (ci.TwoLetterISOLanguageName == "it")
                {
                    ViewBag.Message = "Italian";
                    return RedirectToAction("Italian");
                }
            }
        }
        catch (CultureNotFoundException)
        {
            ci = CultureInfo.InvariantCulture;
        }
    }
    else
    {
        ci = CultureInfo.InvariantCulture;
        ViewBag.Lang = ci + " 1";
        ViewBag.Message = "English";
    }

    return View();
}

It works; however, in Google Search Console, I get this error

Page fetch Failed: Server error (5xx)

Edit: I narrowed the problem to: '''' ci = new CultureInfo(userLanguages[0]); ''''

0

There are 0 best solutions below