Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions aspnetcore/diagnostics/bl0008.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
title: "BL0008: Component parameters should be auto properties"
description: "Learn about analysis rule BL0008: Component parameters should be auto properties"
author: guardrex
monikerRange: '>= aspnetcore-3.1'
ms.author: wpickett
ms.date: 11/17/2025
uid: diagnostics/bl0008
---
# BL0008: Component parameters should be auto properties

| | Value |
| - | - |
| **Rule ID** | BL0008 |
| **Category** | Usage |
| **Fix is breaking or non-breaking** | Non-breaking |

## Cause

A property has the [`[SupplyParameterFromForm]` attribute](xref:Microsoft.AspNetCore.Components.SupplyParameterFromFormAttribute) and is initialized with a property initializer.

## Rule description

The property can be overwritten with `null` when the component receives parameters, causing it to lose its initial value.

## How to fix violations

To ensure the initialized value isn't overwritten, move the initialization to one of the [`OnInitialized{Async}` lifecycle methods](xref:blazor/components/lifecycle#component-initialization-oninitializedasync).

Consider the following Razor component that generates a BL0008 code analysis warning because the `Model` for the form is decorated with `[SupplyParameterFromForm]` and has a property initializer:

```razor
<EditForm Model="Model" OnSubmit="Submit" FormName="Starship1">
...
</EditForm>
@code {
[SupplyParameterFromForm]
private Starship Model { get; set; } = new();
private void Submit()
{
...
}
public class Starship
{
public string? Id { get; set; }
}
}
```

To fix the violation, the initialization code for the `Model` property is moved to the [`OnInitialized` lifecycle method](xref:blazor/components/lifecycle#component-initialization-oninitializedasync), leaving the property as an [automatically implemented property (*auto property*)](/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties). The following change also makes `Model` a [nullable reference types (NRT)](xref:migration/50-to-60#nullable-reference-types-nrts-and-net-compiler-null-state-static-analysis):

```csharp
[SupplyParameterFromForm]
private Starship? Model { get; set; }

protected override void OnInitialized() => Model ??= new();
```

## When to suppress warnings

Do not suppress a warning from this rule.
2 changes: 2 additions & 0 deletions aspnetcore/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,8 @@ items:
uid: diagnostics/bl0006
- name: BL0007
uid: diagnostics/bl0007
- name: BL0008
uid: diagnostics/bl0008
- name: MVC1000
uid: diagnostics/mvc1000
- name: MVC1001
Expand Down