I've written a HtmlHelper extension method which reads content of CSS file and wraps it into tag. See the code below:
public static IHtmlString EmbedCss(this HtmlHelper htmlHelper, string cssFile)
{
var httpContext = htmlHelper.ViewContext.RequestContext.HttpContext;
var physicalCssFileName = httpContext.Server.MapPath(cssFile);
if (!File.Exists(physicalCssFileName))
{
throw new FileNotFoundException(string.Format("Requested CSS file \"{0}\" is not found", physicalCssFileName));
}
var css = File.ReadAllText(physicalCssFileName);
var tb = new TagBuilder("style");
tb.Attributes["type"] = "text/css";
tb.SetInnerText(css.TrimStart(' ').TrimEnd(' '));
return MvcHtmlString.Create(tb.ToString());
}
Then I call this method in a view:
@Html.EmbedCss("~/assets/css/critical.css")
The quote and double-quoted characters in the output are being encoded as $#39; and $quot; respectively. If I replace the call of the extension method with the following code the quotation characters in the output aren't being encoded.
<style>@Html.Raw(File.ReadAllText(Server.MapPath(("~/assets/css/critical.css"))))</style>
Could anybody explain why it happens?
Aucun commentaire:
Enregistrer un commentaire