C# Razor Loading a css stylesheet form the server and embedding it in the view

38 Views Asked by At

I would like to bring an external stylesheet into my web application's _layout.cshtml view.

I have so far been able to read it from wwwroot into a variable but I have hit an encoding issue.

My basic non-robust code so far is this

@inject IWebHostEnvironment _hostingEnvironment

....

string path = string.Concat(_hostingEnvironment.WebRootPath, "\\styles\\basic.min.css");
string text = await File.ReadAllTextAsync(path, Encoding.Default);
var styleText = HttpUtility.HtmlDecode(text);

<style type="text/css">
@styleText
</style>

But the resulting string contains

&quot; 

instead of the " character despite my attempts to decode it.

I tried this exact code in a DotNet fiddle and of course it returns the correctly decoded string.

The only reason I can think of is that because it's inside the head of a HTML document, the resulting string is affected by the meta charset tag

<meta charset="UTF-8">
0

There are 0 best solutions below