CsharpTags.Core 1.0.0-beta-4

This is a prerelease version of CsharpTags.Core.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package CsharpTags.Core --version 1.0.0-beta-4
                    
NuGet\Install-Package CsharpTags.Core -Version 1.0.0-beta-4
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CsharpTags.Core" Version="1.0.0-beta-4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CsharpTags.Core" Version="1.0.0-beta-4" />
                    
Directory.Packages.props
<PackageReference Include="CsharpTags.Core" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CsharpTags.Core --version 1.0.0-beta-4
                    
#r "nuget: CsharpTags.Core, 1.0.0-beta-4"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CsharpTags.Core@1.0.0-beta-4
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CsharpTags.Core&version=1.0.0-beta-4&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=CsharpTags.Core&version=1.0.0-beta-4&prerelease
                    
Install as a Cake Tool

CsharpTags.Core

A type-safe HTML generation library for C# that provides a fluent, functional approach to building HTML documents with compile-time safety.

Features

  • Type-safe HTML construction - Compile-time validation of HTML structure
  • Functional API - Fluent interface with immutable data structures
  • HTML Encoding - Automatic encoding of text content and attributes
  • Comprehensive HTML5 Support - Full coverage of HTML5 elements and attributes
  • ARIA Accessibility - Built-in support for ARIA attributes
  • CSS Unit Extensions - Type-safe CSS measurement units

Installation

<PackageReference Include="CsharpTags.Core" Version="1.0.0-beta-4" />

Quick Start

using CsharpTags.Core.Types;
using static CsharpTags.Core.Types.Prelude;

// Create a simple HTML document
var html = Html.Child(
    Head.Child(
        Title.Child("My Page"),
        Meta.Attr(Charset << "UTF-8")
    ),
    Body.Child(
        Div.Attr(Class << "container").Child(
            H1.Child("Welcome to CsharpTags"),
            P.Child("This is a type-safe HTML generation example."),
            RawStr("<p>This is raw html</p>"),
            Button.Attr(
                Id << "submit-btn",
                Class << "btn btn-primary",
                Disabled_ << false
            ).Child("Click Me")
        )
    )
);

string result = html.Render();

Asp Net Core

If you install

<PackageReference Include="CsharpTags.AspNetCore" Version="1.0.0-beta-4" />

And add the import

using CsharpTags.AspNetCore;

You will have access to the extension methods .ToActionResult() and .ToResult()

If you use minimal apis you can do something similar to that of Carter.

Carter

Having the CsharpTags.AspNetCore package you can register the filter Carter like this:

var group = app.MapGroup(string.Empty).AddEndpointFilter<HtmlElementEndpointFilter>();
group.MapCarter();

Core Concepts

HTML Elements

The library provides all standard HTML5 elements as static properties:

var div = Div.Child("Hello World");
var link = A.Attr(Href << "/page.html").Child("Click here");
var image = Img.Attr(Src << "photo.jpg", Alt << "A photo");

Attributes

Type-safe attributes with proper encoding:

// String attributes (automatically encoded)
var div = Div.Attr(Class << "container<test>"); // becomes class="container&lt;test&gt;"

// Boolean attributes (presence-based)
var input = Input.Attr(Disabled_ << true); // becomes <input disabled />
var input2 = Input.Attr(Disabled_ << false); // becomes <input />

// Integer attributes
var input3 = Input.Attr(TabIndex << 5); // becomes tabindex="5"

Text Content

Automatic HTML encoding for safe text rendering:

var safeText = new Str { Value = "<script>alert('xss')</script>" };
// Renders as: &lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;

Lists

Combine multiple elements:

var items = new HtmlElement[]
{
    Li.Child("Item 1"),
    Li.Child("Item 2"),
    Li.Child("Item 3")
};

var list = Ul.Child(items.ToHtml());

Usage

CSS Units

Type-safe CSS measurements using number extensions:

var width = 100.Px;        // "100px"
var height = 50.Pct;       // "50%"
var fontSize = 1.2.Rem;    // "1.2rem"
var rotation = 45.Deg;     // "45deg"

Custom Data Attributes

var element = Div.Attr(
    DataAttr("user-id") << "12345",
    DataAttr("role") << "admin"
);
// Renders as: <div data-user-id="12345" data-role="admin"></div>

ARIA Attributes

Full accessibility support:

var button = Button.Attr(
    Label << "Submit form",
    DescribedBy << "submit-help",
    Disabled << false
).Child("Submit");

Building Complex Structures

var page = Html.Child(
    Head.Child(
        Title.Child("Product Page"),
        Meta.Attr(Charset << "UTF-8"),
        Link.Attr(
            Rel << "stylesheet",
            Href << "/css/styles.css"
        )
    ),
    Body.Child(
        Header.Attr(Class << "site-header").Child(
            Nav.Child(
                Ul.Child(
                    Li.Child(A.Attr(Href << "/").Child("Home")),
                    Li.Child(A.Attr(Href << "/about").Child("About")),
                    Li.Child(A.Attr(Href << "/contact").Child("Contact"))
                )
            )
        ),
        Main.Child(
            Article.Child(
                H1.Child("Product Name"),
                Img.Attr(
                    Src << "product.jpg",
                    Alt << "Product image",
                    Width_ << 400,
                    Height_ << 300
                ),
                P.Child("Product description..."),
                Button.Attr(
                    Class << "buy-btn",
                    Id << "purchase-button"
                ).Child("Add to Cart")
            )
        )
    )
);

License

Copyright (c) Frairlyn Camilo Roque Suarez. All rights reserved.

Contributing

This library is designed with extensibility in mind. Feel free to submit issues and pull requests for additional HTML elements, attributes, or features.


Note: This is a beta release. API may change before version 1.0.0.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on CsharpTags.Core:

Package Downloads
CsharpTags.Htmx

Provides all the keys used by base htmx v1

CsharpTags.AspNetCore

Provides methods for converting HtmlContent into AspNet Core and MVC types

CsharpTags.Carter

Seamless integration between CsharpTags and Carter for ASP.NET Core Minimal APIs

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0-beta-4 41 3/7/2026
1.1.0-beta-3 58 2/15/2026
1.1.0-beta-2 54 2/2/2026
1.1.0-beta-1 71 1/6/2026
1.0.0-beta-5 407 12/10/2025
1.0.0-beta-4 394 12/9/2025
1.0.0-beta-3 154 11/26/2025
1.0.0-beta-2 145 11/25/2025
1.0.0-beta-1 110 11/23/2025