AFract
AFract

Reputation: 9700

Razor code formatting issue with Visual Studio 2022

With Visual Studio 2022 (across multiple minor/patch versions) and ReSharper 2021.3, I have a formatting issue with C# tags in Razor (.cshtml) views.

Given the following existing code:

Before

If I type Ctrl + K + D (format entire document), Visual Studio produces the following result:

After

The HTML tags formatting is fine, but the Razor tags formatting is a lot harder to read, especially for nested lambdas at the bottom.

I have tried different things, but I can't figure out the proper option in Visual Studio Settings or ReSharper Settings to manage these rules.

Upvotes: 10

Views: 10236

Answers (2)

Sayed Mahmoud
Sayed Mahmoud

Reputation: 138

I had the same issue, and this solution fixed it: Use legacy Razor editor for ASP.NET Core.

  • Go to Tools → Options → Text Editor → HTML → Advanced.
  • Set “Use legacy Razor editor for ASP.NET Core” to true.
  • Restart Visual Studio.

Text Editor-HTML-Advanced option

Upvotes: 3

Petr Bohac
Petr Bohac

Reputation: 35

I postponed upgrading to .NET 6 because of this. It's definitely a bug and is not linked to ReSharper or .NET versions; it's the IDE itself.

You can see the bug report here on the developer community page:

VS 2022 Razor Editor not formatting HTML

And on GitHub:

VS 2022 Razor Editor not formatting HTML #8006

The issue was marked as stale due to lack of feedback by the author. New activity on the GitHub post with more relevant information about the bug may trigger a new investigation.

Upvotes: 1

Related Questions