How to get Sitecore home address?

How can I just get the http://www.mysite.com/en body of the main page URL of a site without an element path? Ultimately, I want to add Model.Url to

It:

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 "/ student / business / new-information-landing pages / Akron-stories"

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

Gives me a "/"

+6
source share
1 answer

You can set the parameter in web.config on 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 install 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 linkmanager options

+12
source

Source: https://habr.com/ru/post/959269/


All Articles