dotnet / dotnet/aspnetcore

System.NotSupportedException: No data is available for encoding 1252 in .NET Core

Open
#60,706 1 comment 0 reactions 0 assignees View on GitHub
area-networking
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

I am encountering an issue when trying to use Windows-1252 encoding in a .NET Core project. Despite registering CodePagesEncodingProvider, some encoding properties such as IsBrowserDisplay throw a System.NotSupportedException.

### Expected Behavior

Windows-1252 encoding should be fully supported when CodePagesEncodingProvider is registered.

### Steps To Reproduce

Create a new .NET Core project
Install the Nuget package(dotnet add package System.Text.Encoding.CodePages)
Modify the csproj file to include the package
Update the Program.cs file as follows:
using System;
using System.Text;

class Program
{
static void Main()
{
// Register the code pages encoding provider
// Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Encoding.RegisterProvider(provider: CodePagesEncodingProvider.Instance);

Encoding encoding1252 = Encoding.GetEncoding(1252);
if (encoding1252 != null)
{
Console.WriteLine("Encoding 1252 is present.");
// Print encoding details
Console.WriteLine($"Encoding Name: {encoding1252.EncodingName}");
Console.WriteLine($"Web Name: {encoding1252.WebName}");
Console.WriteLine($"Body Name: {encoding1252.BodyName}");
Console.WriteLine($"Header Name: {encoding1252.HeaderName}");
Console.WriteLine($"Code Page: {encoding1252.CodePage}");
Console.WriteLine($"Is Single Byte: {encoding1252.IsSingleByte}");
Console.WriteLine($"Is Browser Display: {encoding1252.IsBrowserDisplay}");
Console.WriteLine($"Is Browser Save: {encoding1252.IsBrowserSave}");
Console.WriteLine($"Is Mail News Display: {encoding1252.IsMailNewsDisplay}");
Console.WriteLine($"Is Mail News Save: {encoding1252.IsMailNewsSave}");
}
else
{
Console.WriteLine("Encoding 1252 is not present.");
}
}
}

### Exceptions (if any)

The encoding is retrieved successfully.
However, when accessing IsBrowserDisplay, a NotSupportedException is thrown.

### .NET Version

9.0.103

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.