4

In EPiServer CMS 7, a Content Area can be tagged with one or more Tags:

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

One way you can wire up Page Types and Tags to create a controller with a TemplateDescriptor attribute:

[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, the SitePageDataController could have been chosen due to two tags. Is there any way to find out at runtime which tag resulted in the current controller being chosen?

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

Greg B
  • 14,597
  • 18
  • 87
  • 141

2 Answers2

2

I know 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 can be null)

inside you action. This blog post describes it a little more in detail http://world.episerver.com/blogs/Anders-Hattestad/Dates/2014/3/EPiServer-7-and-MVC-Views-using-Tags/

Andreas
  • 2,336
  • 2
  • 28
  • 45
1

As far as I can see the tag value is not sent along with the request to the partial controller, so there is no way to find the tag out of the box.

A workaround is to hook into the TemplateResolved event that is called down the pipe, and add the tag name to the route values. That way you can just add a parameter to your action named "tag" and it will be populated with the current 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) { }
}

You might want to choose a different name than "tag" if you use it for something else.

In your controller action just add a tag parameter:

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

    return PartialView(currentPage);
}
aolde
  • 2,287
  • 16
  • 19