0

I am using Razor as a text template engine and I need to initialize a few variables at the top of my cshtml file.

@{ModelContext M = Model;}

The problem is that the above statement adds a \r\n to the output.

This behaviour applies to any type of output. I expect I need to use another Razor tag or syntax that does not become part of the output. For example @using does not produce any output.

Is there any way to have a block of code without inserting a new line to the output ?

Allan Xu
  • 7,998
  • 11
  • 51
  • 122
  • where is it adding ? what is your output ? – Shyju Aug 12 '16 at 19:38
  • The tag I mentioned becomes a \r\n. This behaviour applies to any type of output. I expect I need to use another Razor tag or syntax that does not become part o the output. For example @using does not produce any output. – Allan Xu Aug 12 '16 at 19:42

1 Answers1

0

@using initiates a text/string writer function which replaces the statement with html code.

@{var M = Model;}

Is a c# statement only so when RazorEngine compiles the class, it does not remove @{} statements from the compiled template as this could have flow on effects when generating the class.

Therefore, the only real way to address this issue is to parse the string returned after the template has been run and here is an example of how to do that.

Community
  • 1
  • 1
timkly
  • 793
  • 6
  • 14