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 "/"
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.
The default is filePath . Specifies if Sitecore lowercases the URLs it generates. The default is false .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With