Skip to content

Advanced Usage

As you may be aware, the method ProcessAsync is an essential part for implementing the functionality of a StaticComponent, which is ultimately just a fancy tag helper.

If all you need to do is render a partial view as part of the functionality of your tag helper, then you are fine using the default implementation included in StaticComponent. However, if for some reason you need to do more with your tag helper, then you can override the default implementation.

Doing this will cause the rendering of the corresponding partial view to not happen, however, there are a couple of helper functions included in the base class that you can use to do render the partial view. It would look something like this:

using Microsoft.AspNetCore.Razor.TagHelpers;
using TechGems.RazorComponentTagHelpers;
namespace Sample.Views;
[HtmlTargetElement("hello-world")] //Using a custom name instead of the generated default
public class HelloWorldComponent : StaticComponent
{
public HelloWorldComponent() : base("~/Views/HelloWorld.cshtml") //Specifying a route instead of using the inferred default route view
{
}
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
//Your custom logic here...
await base.RenderPartialView(output);
}
}

The one thing that is worthy of note is that the implementation of RenderPartialView method does use the output and sets a couple of things for partial view rendering to work, specifically:

PropertySet toPurpose
output.ContentThe rendered partial view HTMLThe actual component output.
output.TagNamenullRemoves the wrapper tag so only the partial’s HTML is emitted.

If you need to avoid using RenderPartialView altogether, you can by using the HtmlHelper method inside the base class directly by calling inside your implementation of ProcessAsync. That said RenderPartialView will fit 99% of use cases.

Conditional rendering with an alternate view

Section titled “Conditional rendering with an alternate view”
using Microsoft.AspNetCore.Razor.TagHelpers;
using TechGems.RazorComponentTagHelpers;
namespace Sample.Views;
[HtmlTargetElement("hello-world")] //Using a custom name instead of the generated default
public class HelloWorldComponent : StaticComponent
{
public HelloWorldComponent() : base("~/Views/HelloWorld.cshtml") //Specifying a route instead of using the inferred default route view
{
}
[HtmlAttributeName("render-alternate")]
public bool RenderAlternateView { get; set; }
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
//Your custom logic here...
if(!RenderAlternateView)
{
await base.RenderPartialView(output);
}
else
{
var model = new ModelType() {
Name = "Test",
Message = "Sample message"
};
await base.RenderPartialView("~/Views/DifferentView.cshtml", output, model);
}
}
}