Just forgot to mention that I work on a external package, so perhaps don't have acces to helpers... I have something like this:
publicclassMyHandler : IHttpHandler
{
publicvoid 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:
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
'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...
'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...
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"
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.
Just forgot to mention that I work on a external package, so perhaps don't have acces to helpers... I have something like this:
Is the same information as the root site properties in umbraco backoffice as "link to document" to add:
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
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... (
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
'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...
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
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.
( 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...
is working on a reply...