Skip to main content
All docs
V22.2
  • 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.