Copied to clipboard

Flag this post as spam?

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


  • serhio 22 posts 82 karma points
    Apr 16, 2015 @ 12:01
    serhio
    0

    Access the site "root" URL (for multilingual)

    I have a multilingual site like "mysite.com/en" 

    How should I get this value by code?

     

    When I try HttpContext.Current.Request.ServerVariables["HTTP_HOST"] I get just the "mysite.com"

  • Richard Terris 273 posts 715 karma points
    Apr 16, 2015 @ 12:11
    Richard Terris
    0

    I generally have a helper class which finds the site home node from the current node.

    Something like UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current); _siteRoot = helper.TypedContentAtRoot().FirstOrDefault(x => x.DocumentTypeAlias == "homepage");

    Obviously the home node alias may be different for you but that should find the root of any site you're in based on the current page.

  • serhio 22 posts 82 karma points
    Apr 16, 2015 @ 12:23
    serhio
    0

    Just forgot to mention that I work on a external package, so perhaps don't have acces to helpers... I have something like this: 

    public class MyHandler : IHttpHandler
    { 
        public void ProcessRequest(HttpContext context) {    
    // here I obtain mysite.com
    var siteRoot = HttpContext.Current.Request.ServerVariables["HTTP_HOST"];
    // I need to obtain "mysite.com/en"

    }

    Is the same information as the root site properties in umbraco backoffice as "link to document" to add: 

  • Richard Terris 273 posts 715 karma points
    Apr 16, 2015 @ 12:35
    Richard Terris
    0

    You must have a reference to Umbraco.Web though ?

    If so then you can 'new up' an UmbracoHelper and pass the current page id into your method?

    Alternatively you can get the current page from the URL

    UmbracoContext.Current.ContentCache.GetByRoute(the url from your httpcontext object) 
    
  • serhio 22 posts 82 karma points
    Apr 16, 2015 @ 13:51
    serhio
    0

    Good remark... Unfortunately I have Umbraco v 4.7.2 that does not have ContentCache in umbraco.presentation.UmbracoContext.Current

    UmbracoHelper didn't find either... (

  • Richard Terris 273 posts 715 karma points
    Apr 16, 2015 @ 14:01
    Richard Terris
    0

    You'll be able to use Ucomponents Uquery for the same thing

    https://our.umbraco.org/documentation/Reference/Querying/uQuery/Content/Nodes

    I can't remember which version of Umbraco moved this into the core, if it's there it will be in Umbraco.Core namespace (I think), otherwise you can install ucomponents to get it

    You'll see in the link I sent there's a GetNodeByUrl() method which you will be able to use to get the current page. Then you'll be able to go up the tree and get the root based on that, for any site

  • serhio 22 posts 82 karma points
    Apr 16, 2015 @ 14:34
    serhio
    0
    public class MyHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
                //HttpContext.Current.Request.ServerVariables["HTTP_HOST"]
                string requestUrl = HttpContext.Current.Request.RawUrl;
                var rootNode = uComponents.Core.uQuery.GetNodeByUrl(requestUrl);
                string rootUrl = rootNode.Url;

    'uComponents.Core.uQuery.GetNodeByUrl("http://mysite.com/en/page-xxx/")' threw an exception of type 'System.Xml.XPath.XPathException'
        base: {"Expression must evaluate to a node-set."}
        Message: "Expression must evaluate to a node-set."

    .... RawUrl by the way returns only the relative path...

  • serhio 22 posts 82 karma points
    Apr 16, 2015 @ 14:38
    serhio
    0

    Is there a way to find out what code recuperates the "/en" link in the Back Office image above?

     

    In a page content I'd be able to recuperate it via: 

    "http://"+Context.Request.ServerVariables["HTTP_HOST"]+SubLayout.CurrentModule.AncestorOrSelf(1).Url

  • Richard Terris 273 posts 715 karma points
    Apr 16, 2015 @ 16:10
    Richard Terris
    0

    Taking a look at other posts on uQuery such as this: https://our.umbraco.org/projects/backoffice-extensions/ucomponents/questionssuggestions/29702-uQueryGetNodeByUrl%28%29-usage-errorbug

    suggests that the issue may be the URL you're passing.

    Looks like you need to strip off the host name before passing it to that method.

    It should work if you give it the right parameter.

  • serhio 22 posts 82 karma points
    Apr 16, 2015 @ 17:53
    serhio
    100
    'uComponents.Core.uQuery.GetNodeByUrl("/fr/pages/vos-contacts-commerciaux")' threw an exception of type 'System.Xml.XPath.XPathException'
        base: {"Expression must evaluate to a node-set."}... 
    

    ( so I need to workaround my problem... in fact I have to redirect the "DynamicRobots" (that edits the robots.txt site pointing to the "right" sitemap for a multisite umbraco installation).... but the multiLIGUAL solution with redirects to mysite.com/en, mysite.com/fr etc was not taken into consideration to point to mysite.com/en/sitemap ....

    So finally added a redirect from "mysite.com/fr/robots.txt" to "mysite.com/robots.txt?ln=fr", recuperate Request.QueryString and adding to the site host...

Please Sign in or register to post replies

Write your reply to:

Draft