All docs
V21.1
21.1
20.2
20.1
19.2
The page you are viewing does not exist in version 19.2. This link will take you to the root page.
19.1
The page you are viewing does not exist in version 19.1. This link will take you to the root page.
18.2
The page you are viewing does not exist in version 18.2. This link will take you to the root page.
18.1
The page you are viewing does not exist in version 18.1. This link will take you to the root page.
17.2
The page you are viewing does not exist in version 17.2. This link will take you to the root page.

Move to Code Behind (Blazor)

  • 2 minutes to read

Moves code located in a razor page’s code section to a code-behind file (.razor.cs). If the code-behind file does not exist, this refactoring creates it.

Purpose

This refactoring allows you to separate markup and component logic from a razor file into different files. This makes code maintenance easier.

Availability

Available when the caret is in code within the code section of a razor page.

How to Use

  1. Place the caret in any code line within the razor page’s code section.

  2. Use the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions Menu.

  3. Select “Move to Code Behind” from the menu and press Enter.

    Select Move to Code Behind

After execution, this refactoring adds a new code-behind file to the project, if it does not exist. It also creates a partial class and adds all code from a razor page’s code section inside it. If the code section contains leading comments, attributes, and references used by the type, Move to Code Behind adds them to the code-behind file.

Run Move to Code Behind

You can use the “Activate a new file when it is created by refactorings” option (enabled by default) to configure whether CodeRush opens a generated file when it applies this refactoring. Refer to the Code Actions Settings topic for more information.