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.
This refactoring allows you to separate markup and component logic from a razor file into different files. This makes code maintenance easier.
Available when the caret is in code within the code section of a razor page.
How to Use
Place the caret in any code line within the razor page’s code section.
Use the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions Menu.
Select “Move to Code Behind” from the menu and press Enter.
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.
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.