This is a migrated thread and some comments may be shown as answers.
Custom TagHelper to suppress output
1 Answer 24 Views
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 27 Sep 2019, 11:59 PM

Hi, I have a custom TagHelper that I have written to suppress the output of an HTML element. Basically it allows me to provide a comma delimited list of policies and if the user does not pass that check, all output is suppressed. I can add these TagHelpers to a kendo TagHelper, but of course it does not work, as I know Kendo is doing a lot internally. Is there a way to get this to work though?

If I take the if statement out of this class and wrap the Kendo component with it, it will indeed work. I've also tried messing with the Order property of the TagHelper itself, to no avail. One item I am trying to use this on is the <panelbar-item /> to limit navigation options.

The code in the custom TagHelper class is as follows.

public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
    if (!(await _authorizationService.AuthorizeAsync(ViewContext.HttpContext.User, Policy)).Succeeded)
    {
        output.SuppressOutput();
    }
}

1 Answer, 1 is accepted

Sort by
0
Veselin Tsvetanov
Telerik team
answered on 02 Oct 2019, 11:21 AM

Hello Richard,

The Kendo TagHelpers all implement their own version of the public async Task ProcessAsync(TagHelperContext context, TagHelperOutput output). The PanelBarItemTagHelper, for example, implements its override in the following way:

public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
    var type = GetType();
    if (context.Items.ContainsKey(type))
    {
        PreviousInstance = context.Items[type] as PanelBarItemTagHelper;
    }

    context.Items[type] = this;
    var child = await output.GetChildContentAsync();
    AddSelfToParent(context);
    context.Items[type] = PreviousInstance;

    output.TagName = "li";
    output.TagMode = TagMode.StartTagAndEndTag;

    Href = output.Attributes.FirstOrDefault(a => a.Name == "href")?.Value.ToString();
    TagBuilder enhancedItem = EnhanceItem();
    output.MergeAttributes(enhancedItem);

    output.Content.AppendHtml(enhancedItem.InnerHtml);
    output.Content.AppendHtml(child);

    if (Visible.HasValue && !Visible.Value)
    {
        output.SuppressOutput();
    }
}

Having that said, what I would suggest you is to plug your logic directly in the Kendo TagHelpers source code (the Kendo.Mvc\TagHelpers\PanelBar\PanelBarTagHelper.cs class) and use a custom build of the Telerik UI for ASP.NET Core package. The source code is available for download from your Telerik account.

Regards,
Veselin Tsvetanov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Asked by
Richard
Top achievements
Rank 1
Answers by
Veselin Tsvetanov
Telerik team
Share this question
or