Projektanker.RazorComponents 1.3.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Projektanker.RazorComponents --version 1.3.1                
NuGet\Install-Package Projektanker.RazorComponents -Version 1.3.1                
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.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Projektanker.RazorComponents --version 1.3.1                
#r "nuget: Projektanker.RazorComponents, 1.3.1"                
#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.1

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

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"></slot>
</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"></slot>
</div>

You would use it like this:

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

The <fragment> element allows you to place content in a named slot without wrapping it in a container DOM element. This keeps the flow layout of your document intact.

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 3,158 1/25/2023
1.3.2 328 1/3/2023
1.3.1 293 12/22/2022
1.3.0 279 12/21/2022
1.2.0 332 11/16/2022
1.1.1 379 10/29/2022
1.1.0 404 10/26/2022
1.0.0 386 10/26/2022