dotnet / dotnet/aspnetcore

inconsistent file encoding in ProjectTemplates

Open
#57,679 1 comment 0 reactions 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

This is a successor of #11530.

All `.cshtml` and `.razor` files have UTF-8 encoding with BOM. All `.cs` files have no BOM. This in combination with Visual Studio results in [files being saved in ANSII (or system code page) encoding](https://developercommunity.visualstudio.com/t/vs-saves-files-without-bom-as-ansi-instead-of-utf/364271).

We've hit this problem when adding an error to the `ModelState` in the code behind file:

```csharp
ModelState.AddModelError(string.Empty, "This is an error with än umlaut.");
```

Visual Studio will not save this with UTF-8 encoding and this will break the error message in the browser.

### Expected Behavior

To work around the Visual Studio behaviour, all templates files should be in UTF-8 encoding with BOM.

### Steps To Reproduce

1. add a new razor page (either via context menu in VS or `dotnet new page --name Umlaut --namespace YourApp.Pages --output Pages`)
2. in Visual Studio add an Umlaut (`ModelState.AddModelError(String.Empty, "Än Umlaut");`)
3. build on linux `dotnet build -c release`
4. the binary contains a unicode replacement character `�` which will be rendered in the markup

a minimal repro is available at [unicorn-development/aspnetcore57679](https://github.com/unicorn-development/aspnetcore57679)

### Exceptions (if any)

_No response_

### .NET Version

8.0

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by locating ProjectTemplates and inspecting the encoding of its .cshtml, .razor, and .cs files. Reproduce the issue with the listed dotnet new page steps and the linked minimal repro, then verify that generated template files consistently use UTF-8 with BOM and that the umlaut survives a Linux release build.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.