I've searched here and other webpages, but I'm not finding an answer to my issue.
What I'm trying to achieve is that if a user switches between country A
and country B
the language switches but instead of returning them to the homepage I redirect them to the same page they were on (with the updated language). However, the tricky part for me is that if the user switches between country A
and country C
or D
or anything else, we do want to return them to the home page. So only in the event that they are switching between A
and B
I do want to redirect to the same page. (My language switching stuff is working fine, I'm only having issue with the redirects in the different scenarios.)
My method looks like this:
[HttpGet]
public ActionResult SetLanguage(string languageId)
{
var currentCulture = GetCurrentCulture(); //Gets the current language for a user
if ((currentCulture == "A" || currentCulture == "B")
&& (languageId == "A" || language == "B"))
{
// Set culture to use chosen language/culture
CultureService.SaveCulture(HttpContext.Response, languageId);
Response.Redirect(Request.RawUrl);
}
else
{
CultureModule.SavePreferredCulture(HttpContext.Response, languageCode);
}
return RedirectToAction("Index", "Home", new { culture = languageId });
This is causing this error in Chrome:
This webpage has a redirect loop
It appears when debugging I've got my self an infinite loop, but I don't understand why/how, or how to fix this.
Here is what I have tried to change: the Response.Redirect(Request.RawUrl);
to try and return
that, but that doesn't work so I tried to play around with the other Redirect
options such as RedirectPermanent
and the others. I'm not having any luck with this, and it's been a while since I've had to play around with something like this so I'm at a loss here with what I'm doing wrong.
I believe that I need a return
in place of the Response.Redirect(Request.RawUrl);
but I've tried playing with the stuff above and I'm not able to get anything to go.
If you need any other code or additional details please let me know and I'll update the post.
Thank you for your help!