Docs

State & DI

Share logic and data through dependency injection.

Registering services

Program.cs
csharp
builder.Services.AddScoped<CartService>();

Injecting them

Use @inject in markup or [Inject] in code to get a service instance.

razor
razor
@inject CartService Cart

<p>@Cart.Count items</p>