Fawn
Fawn

Reputation: 73

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 "/"

Upvotes: 6

Views: 13903

Answers (1)

dunston
dunston

Reputation: 2670

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

Upvotes: 13

Related Questions