Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 930
AFract Avatar asked Apr 29 '26 14:04

AFract


2 Answers

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.
  • Additionally, you can refer to this answer Intellisense in razor/cshtml files not working (Visual Studio 2022)

Text Editor-HTML-Advanced option

like image 184
Sayed Mahmoud Avatar answered May 03 '26 23:05

Sayed Mahmoud


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.

like image 26
Petr Bohac Avatar answered May 03 '26 21:05

Petr Bohac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!