Skip to content

Commit 8f3a0b2

Browse files
committed
Add BL0008 diagnostic article
1 parent 7a1593e commit 8f3a0b2

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

aspnetcore/diagnostics/bl0008.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
title: "BL0008: Component parameters should be auto properties"
3+
description: "Learn about analysis rule BL0008: Component parameters should be auto properties"
4+
author: guardrex
5+
monikerRange: '>= aspnetcore-3.1'
6+
ms.author: wpickett
7+
ms.date: 11/17/2025
8+
uid: diagnostics/bl0008
9+
---
10+
# BL0008: Component parameters should be auto properties
11+
12+
| | Value |
13+
| - | - |
14+
| **Rule ID** | BL0008 |
15+
| **Category** | Usage |
16+
| **Fix is breaking or non-breaking** | Non-breaking |
17+
18+
## Cause
19+
20+
A property has the [`[SupplyParameterFromForm]` attribute](xref:Microsoft.AspNetCore.Components.SupplyParameterFromFormAttribute) and is initialized with a property initializer.
21+
22+
## Rule description
23+
24+
The property can be overwritten with `null` when the component receives parameters, causing it to lose its initial value.
25+
26+
## How to fix violations
27+
28+
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).
29+
30+
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:
31+
32+
```razor
33+
<EditForm Model="Model" OnSubmit="Submit" FormName="Starship1">
34+
...
35+
</EditForm>
36+
37+
@code {
38+
[SupplyParameterFromForm]
39+
private Starship Model { get; set; } = new();
40+
41+
private void Submit()
42+
{
43+
...
44+
}
45+
46+
public class Starship
47+
{
48+
public string? Id { get; set; }
49+
}
50+
}
51+
```
52+
53+
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):
54+
55+
```csharp
56+
[SupplyParameterFromForm]
57+
private Starship? Model { get; set; }
58+
59+
protected override void OnInitialized() => Model ??= new();
60+
```
61+
62+
## When to suppress warnings
63+
64+
Do not suppress a warning from this rule.

aspnetcore/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,8 @@ items:
14991499
uid: diagnostics/bl0006
15001500
- name: BL0007
15011501
uid: diagnostics/bl0007
1502+
- name: BL0008
1503+
uid: diagnostics/bl0008
15021504
- name: MVC1000
15031505
uid: diagnostics/mvc1000
15041506
- name: MVC1001

0 commit comments

Comments
 (0)