Skip to content

Blazor code-behind: document partial class approach, retire base-class-only guidance#52472

Open
Copilot wants to merge 3 commits intomainfrom
copilot/update-code-behind-syntax
Open

Blazor code-behind: document partial class approach, retire base-class-only guidance#52472
Copilot wants to merge 3 commits intomainfrom
copilot/update-code-behind-syntax

Conversation

Copy link
Contributor

Copilot AI commented Mar 19, 2026

The Code-behind section only described the old base class + @inherits model, which was superseded when Blazor added partial class support. Using the base class pattern causes IDEs to surface both the component and its base class as separate entries in IntelliSense/component lists.

Changes

  • Preferred pattern (new): partial class with the same name as the component — no @inherits, no Base suffix, members can be private
  • Alternative pattern (retained): base class model documented as a secondary option, with a callout about the IDE duplication issue

Preferred partial class pattern

Counter.razor — no @inherits needed:

<h1>Counter</h1>
<p>Current count: @currentCount</p>
<button @onclick="IncrementCount">Click me</button>

Counter.razor.cspartial, same name, private members work:

public partial class Counter : ComponentBase
{
    private int currentCount = 0;

    private void IncrementCount()
    {
        currentCount++;
    }
}

The Razor compiler already emits a partial class for the component, so both parts merge at compile time without any explicit inheritance wiring.

Original prompt

This section details on the original issue you should resolve

<issue_title>Code-Behind Syntax</issue_title>
<issue_description>
2 little details on that page. For code-behind, it recommends to add "Base" to the code-behind file and use @inherit.

By doing so, whenever we use the component and type <MyComponent, it appears double, the base class and the actual class.

It should mention that you can have the code-behind with the same name (without Base), mark it as "partial", and you no longer need @inherit, and it connects the file correctly. This avoids having double-components in the list.

Also for Event Handlers, it could be good to mention that @on... syntax is only for built-in events, and that custom events do not use that syntax. Should custom events start with "On" by convention or not? The syntax is

<MyComponent Processing="Component_Processing" />

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

<agent_instructions>@danroth27's comment is the correct cause for this issue. Add information on partial class support.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@danroth27 It sounds like the content wasn't updated after we added partial class support and is using the old base class model for code-behind files.

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.


Internal previews

📄 File 🔗 Preview link
docs/architecture/blazor-for-web-forms-developers/components.md Build reusable UI components with Blazor

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
Copilot AI changed the title [WIP] Update documentation for code-behind syntax Blazor code-behind: document partial class approach, retire base-class-only guidance Mar 19, 2026
Copilot AI requested a review from BillWagner March 19, 2026 13:19
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM, and is ready for final review.

@BillWagner BillWagner marked this pull request as ready for review March 20, 2026 14:09
@BillWagner BillWagner requested review from a team and danroth27 as code owners March 20, 2026 14:09
Copilot AI review requested due to automatic review settings March 20, 2026 14:09
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the Blazor Web Forms migration guidance to reflect the modern code-behind pattern for Razor components, documenting the preferred partial class approach and demoting the older base-class-only (@inherits) guidance.

Changes:

  • Document the preferred code-behind approach using a partial class with the same name as the component (no @inherits, private members allowed).
  • Retain the base class + @inherits approach as an alternative, and add a callout about IDE duplication in IntelliSense/component lists.
  • Update article metadata (ms.date, and add ai-usage).

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Member

@danroth27 danroth27 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Code-Behind Syntax

4 participants