skip to Main Content

I’m using .NET 8 and Blazor in Visual Studio 2022 and I want to move my C# code to a code behind file. There is supposed to be an option called "Extract block to code behind" if you highlight the code block in the .razor page, I’ve watched a tutorial on this but I don’t have that option in my editor.

Has this been removed from .NET 8 or am I missing a setting of some sort?

It seems to be a general issue with the QuickActions menu, it doesn’t appear if I press Ctrl+. or right click on the menu options

2

Answers


  1. It works OK over here, VS 17.9.3.

    Do not "highlight the code block" but just place your cursor in the @code word and then press Ctrl+.

    Login or Signup to reply.
  2. Visual Studio Information

    Microsoft Visual Studio Enterprise 2022 (64-bit) - Current
    Version 17.9.6
    

    The reason this was not working for me was because there were
    Razor Comments in the @code { } section of the razor page

    Solution

    Only use C# Comments in the @code { } section of the .razor file

    Using Razor Comments in the @code { } section of the razor file makes is so
    Visual Studio won’t provide the Extract block to code behind Quick Action

    Incorrect Example

    Example.razor

    <div>Not Working</div>
    
    @code {
        private void Method()
        {    
            @* This type of comment DOES NOT work                 *@
            @* Extract code block to code behind is NOT available *@
        }
    }
    

    Correct Example

    Example.razor

    <div>This Works</div>
    
    @code {
        private void Method()
        {
            // This type of comment WORKS
            /* This type of comment WORKS */
            // Extract code block to code behind IS available
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search