Projektanker.RazorComponents 1.3.3

dotnet add package Projektanker.RazorComponents --version 1.3.3
NuGet\Install-Package Projektanker.RazorComponents -Version 1.3.3
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="Projektanker.RazorComponents" Version="1.3.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Projektanker.RazorComponents --version 1.3.3
#r "nuget: Projektanker.RazorComponents, 1.3.3"
#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.
// Install Projektanker.RazorComponents as a Cake Addin
#addin nuget:?package=Projektanker.RazorComponents&version=1.3.3

// Install Projektanker.RazorComponents as a Cake Tool
#tool nuget:?package=Projektanker.RazorComponents&version=1.3.3

Razor Components

NuGet

Razor Components is an ASP.NET Core library that allows you to write UI components while maintaining compatibility with Razor Pages and MVC.

Installation

Install the NuGet package

dotnet add package Projektanker.RazorComponents

Razor Components works by using tag helpers.
As with all tag helpers, you will need to go to the _ViewImports.cshtml file and add a reference to the package's and your project's namespace like so:

@addTagHelper *, Projektanker.RazorComponents
@addTagHelper *, Sample.Web

Usage

Basic usage without child content

Given folder structure:

~/Views/Components
  - HelloWorld.cshtml
  - HelloWorld.cshtml.cs

HelloWorld.cshtml.cs:

namespace Sample.Web.Views.Components;

[HtmlTargetElement("HelloWorld")]
public class HelloWorld : RazorComponentTagHelper
{
    public string Greeting { get; set; } = string.Empty;
}

HelloWorld.cshtml:

@using Sample.Web.Views.Components
@model HelloWorld
<p>
  <strong>@Model.Greeting</strong> World
</p>

You would use it like this:

<HelloWorld greeting="Hello" />

Basic usage with single child content

Given folder structure:

~/Views/Components
  - Button.cshtml
  - Button.cshtml.cs

Button.cshtml.cs:

namespace Sample.Web.Views.Components;

[HtmlTargetElement("Button")]
public class Button : RazorComponentTagHelper
{
}

Button.cshtml:

@using Sample.Web.Views.Components
@model Button
<button class="btn btn-primary">
  <slot model="@Model" />
</button>

You would use it like this:

<Button>
  Click me
</Button>

A component can specify fallbacks for any slots that are left empty, by putting content inside the <slot> element:

@using Sample.Web.Views.Components
@model Button
<button class="btn btn-primary">
  <slot model="@Model">
    <em>no content was provided</em>
  </slot>
</button>

Advanced usage with named slots

Given folder structure:

~/Views/Components
  - Card.cshtml
  - Card.cshtml.cs

Card.cshtml.cs:

namespace Sample.Web.Views.Components;

[HtmlTargetElement("Card")]
public class Card : RazorComponentTagHelper
{
}

Card.cshtml:

@using Sample.Web.Views.Components
@model Card
<div class="card">
  <h3>
    <slot name="title" model="@Model">
      Missing title
    </slot>
  </h3>
  <slot name="content" model="@Model" />
</div>

You would use it like this:

<Card>
  <fragment slot="title">
    Card title
  </fragment>
  <fragment slot="content">
    <div>
      <p>Card content</p>
      <p>Lorem ipsum</p>
    </div>
  </fragment>
</Card>

The <fragment> element allows you to place content in a named slot.

Inspiration

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

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.3 1,570 1/25/2023
1.3.2 297 1/3/2023
1.3.1 259 12/22/2022
1.3.0 248 12/21/2022
1.2.0 304 11/16/2022
1.1.1 347 10/29/2022
1.1.0 376 10/26/2022
1.0.0 354 10/26/2022