0

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 RedirectPermanentand 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!

maxshuty
  • 9,708
  • 13
  • 64
  • 77
  • Does your Home method redirect to SetLanguage? If so, what's the logic that causes that redirect? – Necoras Nov 10 '15 at 19:12
  • @Necoras No, that method just returns the `View();` – maxshuty Nov 10 '15 at 19:23
  • Of course it has an infinite loop. Response.Redirect(Request.RawUrl); just means "redirect back here again". – Sentinel Nov 10 '15 at 20:38
  • @Sentinel So what should be done instead of that then? – maxshuty Nov 10 '15 at 20:46
  • Redirect to the original page, not the set language action, which is what you are doing. All your code does is check if language is a and requested language is a, then set language to a and repeat. – Sentinel Nov 10 '15 at 23:27

3 Answers3

0

This error (client side error by the browser) means that you keep redirecting to the same page infinitely.

I think what you are trying to check for redirecting to the same page is:

if((currentCulture == "A" && languageId == "B") || (currentCulture == "B" && languageId == "A"))

And you can try to use Request.Url.AbsolutePath to avoid the query string to be passed again during the redirect. Request.RawUrl contains the query string and languageId is probably passed in it.

Oguz Ozgul
  • 6,809
  • 1
  • 14
  • 26
  • My major issue is with the `Response.Redirect(Request.RawUrl);` as that is causing an infinite loop for me – maxshuty Nov 10 '15 at 19:10
  • That is because you check whether the current culture code is either A or B, and when the user switches to either one, the condition evaluates to true, all the time, and redirects again – Oguz Ozgul Nov 10 '15 at 19:12
  • Just check whether the new language selection lnguageId is different than the current and is one of A or B – Oguz Ozgul Nov 10 '15 at 19:13
  • Raw Ural contains the query string. You can also try Request.Url.AbsolutePath instead – Oguz Ozgul Nov 10 '15 at 19:16
  • So trying what you said leads to the same error. I'll give the `Request.Url.AbsolutePath` now – maxshuty Nov 10 '15 at 19:19
  • I also see that your updated code does not contain the condition in my answer.. Please double check it – Oguz Ozgul Nov 10 '15 at 20:37
  • I tested the conditions out in my new edit and they are the way I would want the conditions to happen, basically if the previous language was A or B and the new requested language is either A or B then I want to return to the same path – maxshuty Nov 10 '15 at 20:48
  • Ok, then has absolute path helped? – Oguz Ozgul Nov 10 '15 at 21:11
  • I have tried that and several variants of it and still nothing. :( I'm lost on this one! – maxshuty Nov 10 '15 at 21:16
  • Did you try redirect to action instead of response.redirect with parameters to redirect to the self? – Oguz Ozgul Nov 10 '15 at 22:15
0

Don't bother with this approach. Edit your route config and add the language into the route. Make your controller inherit from a new base controller. Override the on begin execute in the base controller. In there read your route value and set the culture.

Sentinel
  • 3,582
  • 1
  • 30
  • 44
0

I ended up solving this by doing some trickery on the URL.

TLDR: I grabbed the URL using Request.UrlReferrer.Authority and used string.Replace("A","B") to swap out language codes in the URL.

If the normal url was like this:

http://localhost:0000/EN/Product/Shirt

I wanted to achieve a redirect to that same page but instead of EN I wanted FR or whatever language the user set.

So the new URL needed to look like this:

http://localhost:0000/FR/Product/Shirt

To achieve this I did this:

if ((currentCulture == "A" || currentCulture == "B") && (languageId == "A" || language == "B"))
{
                var urlReferrer = this.Request.UrlReferrer;
                if (urlReferrer != null)
                {
                    var currentUrl = urlReferrer.PathAndQuery;
                    var url = "https://" + Request.UrlReferrer.Authority;

                    if ((currentCultureUI == A) && (languageCode == B))
                    {
                        var aToBPathAndQuery = currentUrl.Replace("A", "B");

                        return Redirect(url + aToBPathAndQuery);
                    }

                var bToAPathAndQuery = usersCurrentUrl.Replace("B", "A");

                return Redirect(url + bToAPathAndQuery);

}

Both A and B are strings contained in resource files so I've changed that up to save space in the question and answer and keep things simpler. Now if those values ever change they can be changed once in the resource file and they are good to go. They can be swapped out for something like A = "English" and B = "Spanish"

maxshuty
  • 9,708
  • 13
  • 64
  • 77