Visual Studio 2002: how to create code behind from a Razor page?

Enrico Rossini 176 Reputation points
2021-11-15T09:37:36.587+00:00

I watched the Visual Studio 2022 launch videos, very new cool features. The presenters showed that it is possible to create from a Blazor component or Razor page with few clicks the code behind for the page.

I can't find how to do that.

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,390 questions
0 comments No comments
{count} vote

4 answers

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,011 Reputation points Microsoft Vendor
    2021-11-16T09:35:52.967+00:00

    Hi @Enrico Rossini ,

    if the C# code is getting a big long and we want to use a code behind file, we can get it instantly using the new extract block with code behind factoring

    As we all known, when we create a Razor component, the component's UI is defined using Razor syntax, which consists of Razor markup, C#, and HTML. like this screenshot:

    149673-image.png

    Since the above component also contains both the Html and the C# code, if the C# code is too long, it is hard to review or manage, so in Visual Studio 2022, there has a new feature, it can extract the content of the @code block to a code behind class file:

    For example, in the above FirstComponent component, assume the C# code is too long, we can move the focus between the @ and code (or use the Ctrl + . key), it will show the "Extract block to code behind" option, after selecting it, it will generate the FirstComponent.razor.cs class file which contains the content of the @code block. Refer this screenshot:

    149732-6.gif


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    10 people found this answer helpful.
    0 comments No comments

  2. Enrico Rossini 176 Reputation points
    2021-11-16T08:12:03.437+00:00

    Thank you for your reply. It is not what I'm looking for. In this video at the Visual Studio 2022 launch event, Angelo Petropoulus said: "if the C# code is getting a big long and we want to use a code behide file, we can get it instantly using the new extract block with code behind factoring".

    https://youtu.be/GhqsLNT1fGA?t=4423

    0 comments No comments

  3. Enrico Rossini 176 Reputation points
    2021-11-16T10:06:27.227+00:00

    I tried to create the code behind from my Razor page. How you can see from the screenshot, I tried to press Ctrl + . but nothing happens.

    149718-example.gif


  4. Enrico Rossini 176 Reputation points
    2021-11-16T10:11:30.977+00:00

    This is my version of Visual Studio

    149765-image.png