.net - Append raw string with RazorTemplatePreprocessor - Stack Overflow

admin2025-04-25  5

In my console application, I have a .cshtml Razor file. In the file properties, I have set RazorTemplatePreprocessor as the Custom Tool meaning it is a so-called "Preprocessed Razor Template" (this was explicitly available to pick in the "New File" dialog in older VS versions). When saving the file, Visual Studio creates a C# class so that at runtime I can transform the Razor template into a string which contains the HTML.

When I have a directive like @Model.GetMyString() in my template, the template engine generates Write(Model.GetMyString()) for the C# class code. The Write method encodes the received string using WebUtility.HtmlEncode. How can I make it so that a call to the WriteLiteral method will be generated instead, as that would preserve the result of GetMyString() as-is?

@Html.Raw is not an option because the preprocessor / template engine doesn't know that directive.

In my console application, I have a .cshtml Razor file. In the file properties, I have set RazorTemplatePreprocessor as the Custom Tool meaning it is a so-called "Preprocessed Razor Template" (this was explicitly available to pick in the "New File" dialog in older VS versions). When saving the file, Visual Studio creates a C# class so that at runtime I can transform the Razor template into a string which contains the HTML.

When I have a directive like @Model.GetMyString() in my template, the template engine generates Write(Model.GetMyString()) for the C# class code. The Write method encodes the received string using WebUtility.HtmlEncode. How can I make it so that a call to the WriteLiteral method will be generated instead, as that would preserve the result of GetMyString() as-is?

@Html.Raw is not an option because the preprocessor / template engine doesn't know that directive.

Share Improve this question edited Jan 21 at 1:39 Rena 36.9k7 gold badges48 silver badges87 bronze badges asked Jan 16 at 7:38 user764754user764754 4,2463 gold badges40 silver badges61 bronze badges 7
  • Have you tried using HtmlString? So @(new HtmlString(Model.GetMyString())) or similar. – Jon Skeet Commented Jan 16 at 7:46
  • @JonSkeet Do you mean System.Web.HtmlString or Microsoft.AspNetCore.Html.HtmlString? I tried the latter from the Microsoft.AspNetCore.Html.Abstractions package, but it did not change the generated output. – user764754 Commented Jan 16 at 7:58
  • The latter - basically I'd expect Razor to know that it doesn't need to escape something that's already in an HtmlString. Can you reproduce the problem in a regular Razor page? (It would also be helpful if you could clarify exactly what happens when you try Html.Raw - perhaps stackoverflow.com/questions/73120769 might be useful?) If you can give enough information to make this easy to reproduce, it would make it easier to help you. – Jon Skeet Commented Jan 16 at 8:05
  • @JonSkeet The generated c# code has this line: as Action<System.IO.TextWriter>. If the result is not null it will append the string without encoding. It seems like HtmlString doesn't fall under that. You mean I should try @(new HtmlString(Model.GetMyString())) in a regular ASP.NET Core Razor Page? – user764754 Commented Jan 16 at 8:15
  • I'm afraid I don't follow exactly what you mean with the generated code - I would strongly suggest you provide enough precise details in your question so that anyone can set up a minimal project that reproduces the problem. (I know I'd be interested in tinkering with it at that point.) – Jon Skeet Commented Jan 16 at 8:35
 |  Show 2 more comments

1 Answer 1

Reset to default 0

Turns out it's possible to just directly call the WriteLiteral method from inside the Razor template:

@model MyNamespace.MyModel
<div>
@{
    WriteLiteral(Model.GetMyString()); // At this position the return
    // value of GetMyString will be appended raw (not encoded)!
}
</div>
转载请注明原文地址:http://anycun.com/QandA/1745547049a90907.html