Edit

Razor editor code actions in Visual Studio

These code actions are available when you edit Razor files in Visual Studio.

Applies to: .razor files and ASP.NET Core MVC Razor view files (.cshtml)

Generate method

You can generate a method directly from a method call in a Razor file.

  1. Place your cursor on a method call that has no matching definition.
  2. Press Ctrl+. to open Quick Actions and Refactorings.
  3. Select Generate method.

Visual Studio creates a method signature based on the call site so you can continue implementing logic.

Remove unnecessary directives

Unused Razor directives are faded in the editor. You can remove them by using a code action.

  1. Place your cursor on a faded @using directive.
  2. Press Ctrl+. to open Quick Actions and Refactorings.
  3. Select Remove unnecessary directives.

In MVC Razor views, this cleanup also applies to unnecessary @addTagHelper directives.

Sort and Consolidate Usings

Use this code action to organize Razor @using directives.

  1. Open a Razor file that contains multiple @using directives.
  2. Press Ctrl+. to open Quick Actions and Refactorings.
  3. Select Sort and Consolidate Usings.

This action alphabetizes Razor @using directives and removes duplicates.