1

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?

5
  • 2
    Umm, what? MVC by default html encodes all the output, and you have to use something like @Html.Raw() to get a non-encoded string. Commented May 15, 2012 at 20:11
  • I am sorry HttpUtility.HtmlEncode works well. I would prefer Razor solution rather than .NET library solution if possible. Commented May 15, 2012 at 20:11
  • Well, one thin gto note here is that I am working outside MVC environment. Just using Razor library and that's it. I actually tried @Html.Raw.....did not work. Commented May 15, 2012 at 20:12
  • In your base class for your email template, you have implementations for Write and WriteLiteral right? If so, you can simply inject that library call into your writes and thus gain the encoding by default on all calls. Commented May 15, 2012 at 20:18
  • 2
    Well, well, It turned out that I was using very old version of RazorEngine.DLL and I updated it with NuGet which gave me v3- beta and it does Html encoding by default. Commented May 15, 2012 at 21:58

1 Answer 1

2

Just use

@Model.name is @Html.Encode("P & G")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.