Search code examples
c#razorhtml-encode

HtmlEncoding using Razor view engine


i am using Razor view engine's dll do to some templating in my code where I have some thing like Hello, @Model.name as input and my view engine would parse it. It works great but I would like it to Html Encode as well. It does not do it. For example, if I have something like "Hello @Model.Name."

And if @Model.Name is "P & G" the I do not want output to be hello P&G I want output to be like hello p & G which Razor view does not do. Is there any way to achieve this? I also tried putting HttpUtility.HtmlEncoding on top of it but that did not do the job as well.

Any suggestions?


Solution

  • Just use

    @Model.name is @Html.Encode("P & G")