Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Sitecore's home url?

How do I just get http://www.mysite.com/en the main part of the Site homepage url without the item path? Ultimately, I want to append the Model.Url to

This:

Sitecore.Links.UrlOptions urlOptions = new Sitecore.Links.UrlOptions();
urlOptions.AlwaysIncludeServerUrl = true;
url = Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Item, urlOptions);

Gives me "http://www.mysite.com/en/undergraduate/business/new-information-landing-pages/Akron-stories"

Model.Url

Gives me "/undergraduate/business/new-information-landing-pages/Akron-stories"

var context = new SitecoreContext();
url = context.GetHomeItem<Base_Page>().URL;

Gives me "/"

like image 827
Fawn Avatar asked Dec 03 '13 20:12

Fawn


People also ask

How do I find a Sitecore URL?

Approach 1 – Fetch Current Page URL in Sitecore We can use the LinkManager. GetItemURL and pass the current item as a parameter to fetch the current page URL.

What is the default URL for an item in Sitecore?

The default is filePath . Specifies if Sitecore lowercases the URLs it generates. The default is false .


1 Answers

You can set the parameter in web.config on the linkprovider

<add name="sitecore" type="Sitecore.Links.LinkProvider, Sitecore.Kernel" addAspxExtension="false" alwaysIncludeServerUrl="false" encodeNames="true" languageEmbedding="Always" languageLocation="filePath" lowercaseUrls="false" shortenUrls="true" useDisplayName="false" />

or set it on your UrlOption

Sitecore.Links.UrlOptions urlOptions = UrlOptions.DefaultOptions;
urlOptions.LanguageEmbedding = LanguageEmbedding.Always;

// Fetch the start item from Site definition
var startItem = Sitecore.Context.Database.GetItem(Sitecore.Context.Site.ContentStartPath);

var url = Sitecore.Links.LinkManager.GetItemUrl(startItem, urlOptions);

Also always use UrlOptions.DefaultOptions to copy the current settings from the linkmanager options

like image 90
dunston Avatar answered Sep 17 '22 17:09

dunston