Skip to content

Instantly share code, notes, and snippets.



Last active Jun 12, 2019
What would you like to do?
Razor C# Example
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.RenderTree;
namespace Presentation.Elements
public abstract class ElementBase : ComponentBase
readonly string _name;
protected ElementBase(string name) => _name = name;
public string StyleType { get; set; }
public string InlineStyle { get; set; }
public RenderFragment ChildContent { get; set; }
protected override void BuildRenderTree(RenderTreeBuilder builder)
builder.OpenElement(0, _name);
builder.AddAttribute(1, "class", StyleType);
builder.AddAttribute(2, "style", InlineStyle);
builder.AddContent(3, ChildContent);
public sealed class Paragraph : ElementBase
public Paragraph() : base("p") {}
public sealed class Container : ElementBase
public Container() : base("div") {}
@using Presentation.Elements
<Paragraph StyleType="myClass">Hello World!</Paragraph>
The above will emit:
<p class="myClass">Hello World!</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment