How to find out which tag triggered the choice of controller?

In EPiServer CMS 7, a content area can be marked with one or more tags:

@Html.PropertyFor(x => x.CurrentPage.MainContent, new { Tag = "ContentTag" })

How can I link page types and tags to create a controller with an attribute TemplateDescriptor:

[TemplateDescriptor(
    TemplateTypeCategory = TemplateTypeCategories.MvcPartialController,
    Default = true,
    Tags = new[] { "ContentTag", "SecondTag" }
    )]
public class SitePageDataController : PageController<SitePageData>
{
    public ActionResult Index(SitePageData currentContent)
    {
        return View(currentContent);
    }
}

In the above example, SitePageDataController could be selected due to two tags. Is there any way to find out at runtime the tag that resulted in the current controller being selected?

Is their API I can call in my controller an action that will get me a tag?

+4
source share
2 answers

I know that this question was asked two years ago, but there is a way. The short answer is to write

var tag = ControllerContext.ParentActionViewContext.ViewData["tag"] as string;

(It may be null)

. http://world.episerver.com/blogs/Anders-Hattestad/Dates/2014/3/EPiServer-7-and-MVC-Views-using-Tags/

+2

, , .

TemplateResolved, , . , "tag", .

[InitializableModule]
[ModuleDependency(typeof(InitializationModule))]
public class SiteInitializer : IInitializableModule {

    public void Initialize(InitializationEngine context) {   
        var templateResolver = ServiceLocator.Current.GetInstance<TemplateResolver>();
        templateResolver.TemplateResolved += OnTemplateResolved;
    }

    private void OnTemplateResolved(object sender, TemplateResolverEventArgs templateArgs) {
        var routeData = templateArgs.WebContext.Request.RequestContext.RouteData;

        if (!string.IsNullOrEmpty(templateArgs.Tag) && templateArgs.SelectedTemplate != null) {
            // add the tag value to route data. this will be sent along in the child action request. 
            routeData.Values["tag"] = templateArgs.Tag;
        }
        else {
            // reset the value so that it doesn't conflict with other requests.
            routeData.Values["tag"] = null;
        }
    }

    public void Uninitialize(InitializationEngine context) { }
    public void Preload(string[] parameters) { }
}

, , "", - .

:

public ActionResult Index(PageData currentPage, string tag) {
    if (!string.IsNullOrEmpty(tag)) {
        return PartialView(tag, currentPage);
    }

    return PartialView(currentPage);
}
+1

All Articles