Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • MrFlo 159 posts 403 karma points
    Dec 29, 2016 @ 13:07
    MrFlo
    0

    Donut cache and language redirection

    Hi,

    I have a multilanguage website and I'm using donutcache. I would like to redirect the user depending their browser language and stored the language in a cookie. Then redirect automatically the user to the correct language ( /fr /nl or if english stay on /) if they are coming from the home page (/).

    I used to have a simple code that checked the cookie and redirect the user to his cookie language or browser one. Similar to this:

    if (Request.Url.AbsolutePath == @"/" || Request.Url.AbsolutePath == "/default.aspx")
    {
        string[] acceptedLanguages = { "en", "fr", "nl" };
        string lang = "en";
    
        if (HttpContext.Current.Request.Cookies["lang"] != null)
        {
            lang = HttpContext.Current.Request.Cookies["lang"].Value;
            if (acceptedLanguages.Contains(lang) && lang != "en")
            {
                Response.Redirect(HttpContext.Current.Request.Cookies["lang"].Value);
            }
        }
        else
        {
            var browserLanguages = HttpContext.Current.Request.UserLanguages;
    
            foreach (string blang in browserLanguages)
            {
                foreach (string alang in acceptedLanguages)
                {
                    if (blang.Contains(alang))
                    {
                        lang = alang;
                        //create cookie
                        var cookie = new HttpCookie("lang", lang);
                        cookie.Expires = DateTime.Now.AddDays(360);
                        cookie.Domain = HttpContext.Current.Request.Url.Host;
                        HttpContext.Current.Response.Cookies.Add(cookie);
                        if (lang != "en")
                        { Response.Redirect(lang); }
    
                    }
                }
            }
        }
    }
    

    The problem is the caching issue, it keeps redirect all the user to the first user choice of course... How and where can I run this code in a donut hole ? If my home controller has to cached and if I don't want to use JS?

    Thanks for your help

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Dec 29, 2016 @ 16:05
    Nicholas Westby
    0

    Is this intended to run only on the homepage? If so, you could create a controller/action method specific to the homepage that then is not decorated with [OutputCache]. That way, the homepage won't be cached and can run dynamic server side logic.

    Alternatively, you could have the JavaScript make a request to a web method that then returns information indicating whether or not the JavaScript should redirect. Based on the cookie, the JavaScript can then avoid calling the web method on subsequent visits.

  • MrFlo 159 posts 403 karma points
    Dec 30, 2016 @ 08:01
    MrFlo
    0

    Hi Nicholas,

    Yes it's only on the home page. The home controller is cached and I want to keep it cached. The javascript is not the best option as the page has to be loaded for nothing before it happen. The problem is that you can not redirect from a child action. I could not cache the main home controller but the inside content by moving it into a surface controller. Is there another easier way ?

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Dec 30, 2016 @ 08:07
    Dave Woestenborghs
    0

    We did something similar, but early in the request pipeline.

    Will post the code later, don't have my laptop with me

  • MrFlo 159 posts 403 karma points
    Dec 30, 2016 @ 16:01
    MrFlo
    0

    Hi Dave,

    Thanks that would be nice! Was it in the route handler like before the controller is rendered ?

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Dec 31, 2016 @ 09:22
    Dave Woestenborghs
    101

    Hi,

    We did this with a custom global.asax

    The code looks like this :

    namespace MyProject.Web.Core {

    /// <summary>
    /// The global.
    /// </summary>
    public class Global : UmbracoApplication
    {
        /// <summary>
        /// The initialization.
        /// </summary>
        public override void Init()
        {
            var application = this as HttpApplication;
            application.PreRequestHandlerExecute += this.PreRequestExecute;
    
            base.Init();
        }
    
        private void PreRequestExecute(object sender, EventArgs e)
        {
            // logic for cookie detection and redirct goes here            
        }
    
    }
    

    }

    And then in your global.asax file in your root change the directive to inherit from your custom global class

    <%@ Application Inherits="MyProject.Web.Core.Global" Language="C#" %>   
    

    Dave

  • MrFlo 159 posts 403 karma points
    Dec 31, 2016 @ 22:51
    MrFlo
    0

    Hi Dave,

    Thanks it works nicely and it's clean thanks a lot!

    Florent

Please Sign in or register to post replies

Write your reply to:

Draft