In order to resolve links, the Linking API must be invoked on the Content Delivery server, when the page is being quested. This is also called frying. This ensures that links are always displayed only if the target page is available (i.e. published).
In DD4T, when we resolve a Component link, we call one of the ResolveLink methods of the ILinkFactory factory.
Since we are using strongly-typed models, it means we can easily include this logic in the model itself. This means we can provide a lazy loaded property on the ModelBase class that calls the ILinkFactory.ResolveLink method using the Id property of the model.
The property ResolvedUrl gives us the URL of the page where this model appears on, if available. Otherwise, it returns null.
In DD4T, when we resolve a Component link, we call one of the ResolveLink methods of the ILinkFactory factory.
public interface ILinkFactory { string ResolveLink(string componentUri); string ResolveLink(string sourcePageUri, string componentUri, string excludeComponentTemplateUri); ICacheAgent CacheAgent { get; set; } ILinkProvider LinkProvider { get; set; } }
Since we are using strongly-typed models, it means we can easily include this logic in the model itself. This means we can provide a lazy loaded property on the ModelBase class that calls the ILinkFactory.ResolveLink method using the Id property of the model.
public class ModelBase : IComparable<ModelBase> { public string Id { get; set; } public ISchema Schema { get; set; } public string Title { get; set; } private string _resolvedUrl; public string ResolvedUrl { get { if (string.IsNullOrEmpty(_resolvedUrl)) { ILinkFactory linkFactory = DependencyResolver.Current.GetService<ILinkFactory>(); _resolvedUrl = linkFactory.ResolveLink(Id); } return _resolvedUrl; } }
The property ResolvedUrl gives us the URL of the page where this model appears on, if available. Otherwise, it returns null.
Comments