Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 25, 2025

This PR addresses a customer request from VS Feedback to provide an option for creating Razor Component item templates that automatically generate a code-behind (.cs) file alongside the .razor file.

Changes Made

New Item Template: razorcomponent-cb

  • Template Name: "Razor Component with Code-Behind"
  • Short Name: razorcomponent-cb
  • Files Generated: Both Component1.razor and Component1.razor.cs
  • Usage: dotnet new razorcomponent-cb --name MyComponent --namespace MyApp.Components

Template Structure

The new template follows the established code-behind pattern used by Razor Pages:

Component1.razor:

<h3>Component1</h3>

@code {

}

Component1.razor.cs:

using Microsoft.AspNetCore.Components;

namespace MyApp.Namespace
{
    public partial class Component1 : ComponentBase
    {

    }
}

Key Features

  • Proper namespace substitution: The --namespace parameter correctly sets the namespace in the code-behind file
  • Component name substitution: Both files are properly renamed based on the --name parameter
  • Clean separation: Markup stays in .razor, logic goes in .razor.cs
  • ComponentBase inheritance: Code-behind uses partial class inheriting from ComponentBase
  • Full localization support: Includes localization files for all supported languages

Testing

  • Added BlazorServerItemTemplateCodeBehind test to validate the new template functionality
  • Verified both original razorcomponent and new razorcomponent-cb templates work independently
  • Confirmed generated components compile successfully without errors

Customer Impact

This change provides developers who prefer the code-behind model with an automatic way to generate both files when creating new Razor components, matching the existing functionality available for Razor Pages. This addresses the specific customer feedback requesting this feature and provides consistency across ASP.NET Core item templates.

The original razorcomponent template remains unchanged, ensuring no breaking changes for existing workflows.

Original prompt

This section details on the original issue you should resolve

<issue_title>Have an option for a new Razor Component item template to automatically create a code-behind (.cs) file</issue_title>
<issue_description>This is a customer report through VS Feedback, where the customer uses code-behind model almost exclusively and wanted to have an option for creating the .cs file automatically when they add a new .razor file using an item template.</issue_description>

<agent_instructions>Look at https://github.com/dotnet/aspnetcore/tree/main/src/ProjectTemplates/Web.ItemTemplates/content/RazorComponent and create an additional RazorComponentCodeBehind item template</agent_instructions>

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

@ We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our [Triage Process](https://github.com/dotnet/aspnetcore/blob/main/docs/TriageProcess.md). @danroth27 We should consider having the same option on our project templates as well. @ Thanks for contacting us.

We're moving this issue to the .NET 9 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.</comment_new>

Fixes #47030

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] Have an option for a new Razor Component item template to automatically create a code-behind (.cs) file Add RazorComponent with Code-Behind item template Sep 25, 2025
Copilot finished work on behalf of javiercn September 25, 2025 10:07
@Copilot Copilot AI requested a review from javiercn September 25, 2025 10:08
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.

Have an option for a new Razor Component item template to automatically create a code-behind (.cs) file
2 participants