Copied to clipboard

Flag this post as spam?

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


  • John 2 posts 72 karma points
    Aug 07, 2019 @ 11:30
    John
    0

    Getting a culture specific content URL from SurfaceController or WebApi

    I have a site set up with multiple cultures and host-names at the root, eg.

    http://mysite.com/uk
    http://mysite.com/us
    http://mysite.com/au

    if i browse to http://mysite.com/us/products, in the razor view I can then get a list of product nodes, all with the correct culture-specific URLs. eg.

    productsNode.DescendantsOfType("product").ToList();
    

    each one has the correct URL for the page culture, eg:
    http://mysite.com/us/products/product-1

    However, if I want to implement a product-search feature, which uses jquery to call a SurfaceController action to return a list of products in a partial view, the SurfaceController has no concept of the current culture, so I get a list of products all with URLs set as (i think) the first domain/culture that umbraco can find (UK in this case)

    eg.
    http://mysite.com/uk/products/product-1

    How can I tell umbraco to give me a content URL specific to a certain culture (in this case, I'd want it to be the culture of the page that is making the ajax/surface request)?

    This seems like a pretty common scenario; anybody using culture url's who is making SurfaceController or WebApi requests for content items will face this problem, but I can't find any documentation on it

  • Sven 34 posts 138 karma points
    Apr 24, 2020 @ 06:36
    Sven
    0

    Hello there,

    Did you find any solution to this ?

    Thank you!

  • andrew shearer 510 posts 659 karma points
    May 18, 2020 @ 00:42
    andrew shearer
    0

    yes - would like to know best practice here too :)

  • Asembli 85 posts 259 karma points
    May 19, 2020 @ 11:01
    Asembli
    0

    Hi, you can always send current lang from document template to surface controller:

        // in document template razor
    string currentLang = Model.Content.GetCulture().TwoLetterISOLanguageName;
    
        // call to surface controller
    Url.Action("DoSomething", "MySurface", new { lang = currentLang })
    
    
    // surface controller
    public ActionResult DoSomething(string lang)
    {
        // set culture
        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(lang);
    
        // call partial view with the same culture as in document template
        return PartialView("~/Views/Partials/SomePartial.cshtml", model);
    }
    

    Regards

Please Sign in or register to post replies

Write your reply to:

Draft