-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Add BL0008 diagnostic article #36361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
guardrex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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. | ||
guardrex marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## 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. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.