RazorFormatter 1.0.0
dotnet tool install --global RazorFormatter --version 1.0.0
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest
dotnet tool install --local RazorFormatter --version 1.0.0
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=RazorFormatter&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
nuke :add-package RazorFormatter --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
RazorFormatter
A comprehensive code formatter for .razor files used in Blazor projects.
Features
- Parse and format .razor files using Microsoft.AspNetCore.Razor.Language
- HTML formatting with intelligent attribute splitting (>3 attributes split to new lines)
- C# code formatting using Roslyn APIs for @code blocks and inline expressions
- CSS formatting for
<style>blocks with proper indentation - Preserves Razor directives (@page, @inject, @using, etc.)
- Maintains inline Razor expressions compact (@onclick, @key, etc.)
- Comment preservation for both HTML and Razor comments
Installation
- Clone the repository
- Build the project:
dotnet build
Usage
Basic formatting
dotnet run -- --in MyComponent.razor --out MyComponent.formatted.razor
Check mode (verify formatting without writing)
dotnet run -- --in MyComponent.razor --check
Custom options
dotnet run -- --in MyComponent.razor --out MyComponent.razor --indent-size 2 --max-attributes 4
Command Line Options
--in, -i(required): Input .razor file path--out, -o: Output .razor file path (if not specified, overwrites input file)--check, -c: Check if the file is formatted without writing changes--indent-size: Number of spaces for indentation (default: 4)--use-tabs: Use tabs instead of spaces for indentation--max-attributes: Maximum number of attributes per line before splitting (default: 3)
Formatting Rules
HTML and Razor Markup
- Standard indentation and spacing
- Attributes split to new lines when exceeding max-attributes threshold
- Self-closing tags are preserved
C# Code Blocks
- @code {} and @{} blocks formatted using C# conventions
- Proper indentation and brace placement
- Preserves C# expressions within attributes
CSS Blocks
- Formats content within
<style>tags - Handles media queries and nested selectors
- Consistent property formatting
Razor Directives
- Kept at the top of the file
- Single-line format maintained
- Proper spacing between directive groups
Example
Input
@page "/home"
<h1>Hello World</h1>
<style>h1{color:red;}</style>
@code {
void OnClick(){
Console.WriteLine("Clicked");
}
}
Output
@page "/home"
<h1>Hello World</h1>
<style>
h1 {
color: red;
}
</style>
@code {
void OnClick()
{
Console.WriteLine("Clicked");
}
}
Testing
Run the unit tests:
dotnet test
Project Structure
RazorFormatter/
├── Core/
│ ├── RazorParser.cs # Razor file parsing
│ ├── RazorFormatter.cs # Main formatter logic
│ └── FormatterOptions.cs # Configuration options
├── Formatters/
│ ├── HtmlFormatter.cs # HTML element formatting
│ ├── CSharpFormatter.cs # C# code formatting
│ └── CssFormatter.cs # CSS formatting
├── Tests/
│ ├── RazorFormatterTests.cs # Unit tests
│ └── TestFiles/ # Sample .razor files
└── Program.cs # CLI entry point
Dependencies
- Microsoft.AspNetCore.Razor.Language - For parsing Razor syntax
- Microsoft.CodeAnalysis.CSharp - Roslyn for C# formatting
- System.CommandLine - CLI argument parsing
- xUnit - Unit testing framework
License
This project is provided as-is for educational and development purposes.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 296 | 10/17/2025 |