November Happy Hour will be moved to Thursday December 5th.

Class TemplateResolver

The template resolver is responsible to find what template to use for a certain type.

Inheritance
System.Object
TemplateResolver
Namespace: EPiServer.Web
Assembly: EPiServer.Cms.AspNetCore.Templating.dll
Version: 12.0.3
Syntax
public abstract class TemplateResolver : Object, ITemplateResolver, ITemplateResolverEvents
Remarks

The template resolver can be extended using TemplateResolving and TemplateResolved events.

Examples

The following code example demonstrates how to exchange the template for mobile requests.

    [InitializableModule]
public class MobileRedirectSample : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
context.Locate.TemplateResolver().TemplateResolved 
    += new EventHandler<TemplateResolverEventArgs>(MobileRedirectSample_TemplateResolved);
}

public void Uninitialize(global::EPiServer.Framework.Initialization.InitializationEngine context)
{
context.Locate.TemplateResolver().TemplateResolved
    -= new EventHandler<TemplateResolverEventArgs>(MobileRedirectSample_TemplateResolved);
}

void MobileRedirectSample_TemplateResolved(object sender, TemplateResolverEventArgs eventArgs)
{
if (eventArgs.ItemToRender != null && eventArgs.ItemToRender is TypedPage &&
    HttpContext.Current.Request.Browser.IsMobileDevice)
{
    var mobileRender = eventArgs.SupportedTemplates
        .SingleOrDefault(r => r.Name.Contains("Mobile") &&
        r.TemplateTypeCategory == eventArgs.SelectedTemplate.TemplateTypeCategory);

    if (mobileRender != null)
    {
        eventArgs.SelectedTemplate = mobileRender;
    }
}
}

public void Preload(string[] parameters)
{
}
}

Constructors

TemplateResolver()

Exposed for easier mock

Declaration
protected TemplateResolver()

TemplateResolver(IHttpContextAccessor, IContentTypeRepository)

Initializes a new instance of the TemplateResolver abstract.

Declaration
public TemplateResolver(IHttpContextAccessor httpAccessor, IContentTypeRepository contentTypeRepository)
Parameters
Type Name Description
Microsoft.AspNetCore.Http.IHttpContextAccessor httpAccessor
IContentTypeRepository contentTypeRepository

Methods

GetContentType(IContentData)

Get content type for given contentData

Declaration
protected virtual ContentType GetContentType(IContentData contentData)
Parameters
Type Name Description
IContentData contentData
Returns
Type Description
ContentType

HasPageTemplate(PageData)

Determines whether the provided content data item has a page template.

Declaration
public virtual bool HasPageTemplate(PageData pageData)
Parameters
Type Name Description
PageData pageData

The page data that should be checked if it has a template.

Returns
Type Description
System.Boolean

true if the specified render context has template; otherwise, false.

HasPageTemplate(PageData, String)

Determines whether the provided content data item has a page template.

Declaration
public virtual bool HasPageTemplate(PageData pageData, string tag)
Parameters
Type Name Description
PageData pageData

The page data that should be checked if it has a template.

System.String tag

The tag to use when selecting renderer.

Returns
Type Description
System.Boolean

true if the specified render context has template; otherwise, false.

HasTemplate(IContentData, TemplateTypeCategories)

Determines whether the provided content data item has a template matching the provided template type.

Declaration
public virtual bool HasTemplate(IContentData contentData, TemplateTypeCategories templateTypeCategory)
Parameters
Type Name Description
IContentData contentData

The content data.

TemplateTypeCategories templateTypeCategory

The category of template type that should be checked for.

Returns
Type Description
System.Boolean

true if the specified render context has template; otherwise, false.

HasTemplate(IContentData, TemplateTypeCategories, ContextMode)

Determines whether the provided content data item has a template matching the provided template type in given ContextMode

Declaration
public virtual bool HasTemplate(IContentData contentData, TemplateTypeCategories templateTypeCategory, ContextMode contextMode)
Parameters
Type Name Description
IContentData contentData

The content data.

TemplateTypeCategories templateTypeCategory

The category of template type that should be checked for.

ContextMode contextMode

The context mode.

Returns
Type Description
System.Boolean

true if the specified render context has template; otherwise, false.

HasTemplate(IContentData, TemplateTypeCategories, String)

Determines whether the provided content data item has a template matching the provided template type.

Declaration
public virtual bool HasTemplate(IContentData contentData, TemplateTypeCategories templateTypeCategory, string tag)
Parameters
Type Name Description
IContentData contentData

The content data.

TemplateTypeCategories templateTypeCategory

The category of template type that should be checked for.

System.String tag

The tag to use when selecting renderer.

Returns
Type Description
System.Boolean

true if the specified render context has template; otherwise, false.

OnTemplateResolved(TemplateResolverEventArgs)

Called raise the TemplateResolved event when the template has been resolved.

Declaration
protected virtual void OnTemplateResolved(TemplateResolverEventArgs eventArgs)
Parameters
Type Name Description
TemplateResolverEventArgs eventArgs

The event arguments for the event.

OnTemplateResolving(TemplateResolverEventArgs)

Called to raise the TemplateResolving event before attempting to resolve the template.

Declaration
protected virtual void OnTemplateResolving(TemplateResolverEventArgs eventArgs)
Parameters
Type Name Description
TemplateResolverEventArgs eventArgs

The event arguments for the event.

Resolve(HttpContext, ContentType, TemplateTypeCategories)

Resolves what rendering template that should be used for the provided content type given that it is of the provided template type.

Declaration
public virtual TemplateModel Resolve(HttpContext httpContext, ContentType contentType, TemplateTypeCategories templateTypeCategory)
Parameters
Type Name Description
Microsoft.AspNetCore.Http.HttpContext httpContext

The HTTP context.

ContentType contentType

The content type that the template should be associated with.

TemplateTypeCategories templateTypeCategory

The category of template type that should be returned.

Returns
Type Description
TemplateModel

A TemplateModel object that contains information on the template.

Resolve(HttpContext, ContentType, TemplateTypeCategories, String)

Resolves what rendering template that should be used for the provided content type given that it is of the provided template type.

Declaration
public virtual TemplateModel Resolve(HttpContext httpContext, ContentType contentType, TemplateTypeCategories templateTypeCategory, string tag)
Parameters
Type Name Description
Microsoft.AspNetCore.Http.HttpContext httpContext

The HTTP context.

ContentType contentType

The content type that the template should be associated with.

TemplateTypeCategories templateTypeCategory

The category of template type that should be returned.

System.String tag

The tag to use when selecting renderer.

Returns
Type Description
TemplateModel

A TemplateModel object that contains information on the template.

Resolve(HttpContext, Object, TemplateTypeCategories)

Resolves what rendering template that should be used for the provided content item given that it is of the provided template type.

Declaration
public virtual TemplateModel Resolve(HttpContext httpContext, object itemToRender, TemplateTypeCategories templateTypeCategory)
Parameters
Type Name Description
Microsoft.AspNetCore.Http.HttpContext httpContext

The HTTP context.

System.Object itemToRender

The routed item of which type the template should be associated with.

TemplateTypeCategories templateTypeCategory

The category of template type that should be returned.

Returns
Type Description
TemplateModel

A TemplateModel object that contains information on the template.

Resolve(HttpContext, Object, TemplateTypeCategories, ContextMode)

Resolves what rendering template that should be used for the provided content item given that it is of the provided template type and that it has a template for the specified contextMode.

Declaration
public virtual TemplateModel Resolve(HttpContext httpContext, object itemToRender, TemplateTypeCategories templateTypeCategory, ContextMode contextMode)
Parameters
Type Name Description
Microsoft.AspNetCore.Http.HttpContext httpContext

The HTTP context.

System.Object itemToRender

The routed item of which type the template should be associated with.

TemplateTypeCategories templateTypeCategory

The category of template type that should be returned.

ContextMode contextMode

The context mode.

Returns
Type Description
TemplateModel

A TemplateModel object that contains information on the template.

Remarks

When contextMode is Edit first a template with tag Edit is searched for if no such is found a template with no tag is searched for and finally if no is found a template with tag Preview is searched for.

When contextMode is Preview first a template with tag Preview is searched for if no such is found a template with no tag is searched for.

When contextMode is Default or Default first a template with tag Edit is searched for if no such is found a template with no tag is searched for and finally if no is found a template with tag Preview is searched for.

Resolve(HttpContext, Object, TemplateTypeCategories, String)

Resolves what rendering template that should be used for the provided content item given that it is of the provided template type.

Declaration
public virtual TemplateModel Resolve(HttpContext httpContext, object itemToRender, TemplateTypeCategories templateTypeCategory, string tag)
Parameters
Type Name Description
Microsoft.AspNetCore.Http.HttpContext httpContext

The HTTP context.

System.Object itemToRender

The routed item of which type the template should be associated with.

TemplateTypeCategories templateTypeCategory

The category of template type that should be returned.

System.String tag

The tag to use when selecting renderer.

Returns
Type Description
TemplateModel

A TemplateModel object that contains information on the template.

Resolve(HttpContext, Type, Object, TemplateTypeCategories, String)

Resolves what rendering template that should be used for the provided content item given that it is of the provided template type.

Declaration
public virtual TemplateModel Resolve(HttpContext httpContext, Type itemType, object itemToRender, TemplateTypeCategories templateTypeCategory, string tag)
Parameters
Type Name Description
Microsoft.AspNetCore.Http.HttpContext httpContext

The context.

System.Type itemType

Type of the item.

System.Object itemToRender

The routed item of which type the template should be associated with.

TemplateTypeCategories templateTypeCategory

The category of template type that should be returned.

System.String tag

The tag to use when selecting renderer.

Returns
Type Description
TemplateModel

A TemplateModel object that contains information on the template.

Resolve(Object, Type, TemplateTypeCategories, IEnumerable<String>)

Declaration
public TemplateModel Resolve(object item, Type itemType, TemplateTypeCategories category, IEnumerable<string> tags)
Parameters
Type Name Description
System.Object item
System.Type itemType
TemplateTypeCategories category
System.Collections.Generic.IEnumerable<System.String> tags
Returns
Type Description
TemplateModel

ResolveAll(Object, Type, TemplateTypeCategories, IEnumerable<String>)

Resolves what rendering template that should be used for the provided item given that it is of the provided template category.

Declaration
public IEnumerable<TemplateModel> ResolveAll(object item, Type itemType, TemplateTypeCategories category, IEnumerable<string> tags)
Parameters
Type Name Description
System.Object item

The item of which type the template should be associated with.

System.Type itemType

in case item is null then type is used for resolving

TemplateTypeCategories category

The category of template type that should be returned.

System.Collections.Generic.IEnumerable<System.String> tags

The tags to use when selecting a template.

Returns
Type Description
System.Collections.Generic.IEnumerable<TemplateModel>

A TemplateModel object that contains information on the template.

Remarks

The template tags are treated as a prioritized list meaning a template with a tag matching a preceding tag will be preferred.

ResolveAllCore(HttpContext, ContentType, Type, Object, TemplateTypeCategories, IEnumerable<String>)

Template resolver algorithm.

Declaration
protected abstract IEnumerable<TemplateModel> ResolveAllCore(HttpContext httpContext, ContentType contentType, Type itemType, object itemToRender, TemplateTypeCategories category, IEnumerable<string> tags)
Parameters
Type Name Description
Microsoft.AspNetCore.Http.HttpContext httpContext

The HTTP Context (optional, can be null)

ContentType contentType

The content type (optional, can be null)

System.Type itemType

The item type (required)

System.Object itemToRender

The item to render (optional, can be null)

TemplateTypeCategories category

The category (required)

System.Collections.Generic.IEnumerable<System.String> tags

The tags for the template (optional, can be null)

Returns
Type Description
System.Collections.Generic.IEnumerable<TemplateModel>

A template model if found or null if no model was found

ResolveCore(HttpContext, ContentType, Type, Object, TemplateTypeCategories, IEnumerable<String>)

Declaration
protected virtual TemplateModel ResolveCore(HttpContext httpContext, ContentType contentType, Type itemType, object itemToRender, TemplateTypeCategories category, IEnumerable<string> tags)
Parameters
Type Name Description
Microsoft.AspNetCore.Http.HttpContext httpContext

The HTTP Context (optional, can be null)

ContentType contentType

The content type (optional, can be null)

System.Type itemType

The item type (required)

System.Object itemToRender

The item to render (optional, can be null)

TemplateTypeCategories category

The category (required)

System.Collections.Generic.IEnumerable<System.String> tags

The tags for the template (optional, can be null)

Returns
Type Description
TemplateModel

A template model if found or null if no model was found

ResolveMvcTemplate(HttpContext, IContentData)

Resolves what MVC template that should be used for the provided content item.

Declaration
public virtual TemplateModel ResolveMvcTemplate(HttpContext httpContext, IContentData contentItem)
Parameters
Type Name Description
Microsoft.AspNetCore.Http.HttpContext httpContext

The HTTP context.

IContentData contentItem

The content item of which type the template should be associated with.

Returns
Type Description
TemplateModel

A TemplateModel object that contains information on the template.

ResolveMvcTemplate(HttpContext, IContentData, String)

Resolves what MVC template that should be used for the provided content item.

Declaration
public virtual TemplateModel ResolveMvcTemplate(HttpContext httpContext, IContentData contentItem, string tag)
Parameters
Type Name Description
Microsoft.AspNetCore.Http.HttpContext httpContext

The HTTP context.

IContentData contentItem

The content item of which type the template should be associated with.

System.String tag

The tag to use when selecting renderer.

Returns
Type Description
TemplateModel

A TemplateModel object that contains information on the template.

ResolveMvcTemplate(HttpContext, ContentType)

Resolves what MVC template that should be used for the provided content type.

Declaration
public virtual TemplateModel ResolveMvcTemplate(HttpContext httpContext, ContentType contentType)
Parameters
Type Name Description
Microsoft.AspNetCore.Http.HttpContext httpContext

The HTTP context.

ContentType contentType

The content type that the template should be associated with.

Returns
Type Description
TemplateModel

A TemplateModel object that contains information on the template.

ResolveMvcTemplate(HttpContext, ContentType, String)

Resolves what MVC template that should be used for the provided content type.

Declaration
public virtual TemplateModel ResolveMvcTemplate(HttpContext httpContext, ContentType contentType, string tag)
Parameters
Type Name Description
Microsoft.AspNetCore.Http.HttpContext httpContext

The HTTP context.

ContentType contentType

The content type that the template should be associated with.

System.String tag

The tag to use when selecting renderer.

Returns
Type Description
TemplateModel

A TemplateModel object that contains information on the template.

ResolvePageTemplate(HttpContext, IContentData)

Resolves what page template that should be used for the provided content type.

Declaration
public virtual TemplateModel ResolvePageTemplate(HttpContext httpContext, IContentData contentItem)
Parameters
Type Name Description
Microsoft.AspNetCore.Http.HttpContext httpContext

The HTTP context.

IContentData contentItem

The content type that the template should be associated with.

Returns
Type Description
TemplateModel

A TemplateModel object that contains information on the template.

ResolvePageTemplate(HttpContext, IContentData, String)

Resolves what page template that should be used for the provided content type.

Declaration
public virtual TemplateModel ResolvePageTemplate(HttpContext httpContext, IContentData contentItem, string tag)
Parameters
Type Name Description
Microsoft.AspNetCore.Http.HttpContext httpContext

The HTTP context.

IContentData contentItem

The content type that the template should be associated with.

System.String tag

The tag to use when selecting renderer.

Returns
Type Description
TemplateModel

A TemplateModel object that contains information on the template.

ResolvePageTemplate(HttpContext, ContentType)

Resolves what page template that should be used for the provided content type.

Declaration
public virtual TemplateModel ResolvePageTemplate(HttpContext httpContext, ContentType contentType)
Parameters
Type Name Description
Microsoft.AspNetCore.Http.HttpContext httpContext

The HTTP context.

ContentType contentType

The content type that the template should be associated with.

Returns
Type Description
TemplateModel

A TemplateModel object that contains information on the template.

ResolvePageTemplate(HttpContext, ContentType, String)

Resolves what page template that should be used for the provided content type.

Declaration
public virtual TemplateModel ResolvePageTemplate(HttpContext httpContext, ContentType contentType, string tag)
Parameters
Type Name Description
Microsoft.AspNetCore.Http.HttpContext httpContext

The HTTP context.

ContentType contentType

The content type that the template should be associated with.

System.String tag

The tag to use when selecting renderer.

Returns
Type Description
TemplateModel

A TemplateModel object that contains information on the template.

Events

TemplateResolved

Occurs when tries to resolve control. This events raises after finding default or first supported template. One can change the selected template on the TemplateResolverEventArgs

Declaration
public event EventHandler<TemplateResolverEventArgs> TemplateResolved
Event Type
Type Description
System.EventHandler<TemplateResolverEventArgs>
Examples

The following code example demonstrates how to exchange the template for mobile requests.

    [InitializableModule]
public class MobileRedirectSample : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
context.Locate.TemplateResolver().TemplateResolved 
    += new EventHandler<TemplateResolverEventArgs>(MobileRedirectSample_TemplateResolved);
}

public void Uninitialize(global::EPiServer.Framework.Initialization.InitializationEngine context)
{
context.Locate.TemplateResolver().TemplateResolved
    -= new EventHandler<TemplateResolverEventArgs>(MobileRedirectSample_TemplateResolved);
}

void MobileRedirectSample_TemplateResolved(object sender, TemplateResolverEventArgs eventArgs)
{
if (eventArgs.ItemToRender != null && eventArgs.ItemToRender is TypedPage &&
    HttpContext.Current.Request.Browser.IsMobileDevice)
{
    var mobileRender = eventArgs.SupportedTemplates
        .SingleOrDefault(r => r.Name.Contains("Mobile") &&
        r.TemplateTypeCategory == eventArgs.SelectedTemplate.TemplateTypeCategory);

    if (mobileRender != null)
    {
        eventArgs.SelectedTemplate = mobileRender;
    }
}
}

public void Preload(string[] parameters)
{
}
}

TemplateResolving

Occurs when a template is about to be resolved.

Declaration
public event EventHandler<TemplateResolverEventArgs> TemplateResolving
Event Type
Type Description
System.EventHandler<TemplateResolverEventArgs>
Remarks

If an eventHandler sets the TemplateModel on the eventargs to the event then that render is used without calling for registered renderers.

Implements

Extension Methods