This helper will write out the first countFibonacci numbers. It can be called by writing @Fibonacci(30) in the page that defines the helper.

Using Razor syntax in this code looks strange. Razor syntax is designed to write HTML tags. Since I’m printing plain text, I need to use the @: escape (or the <text> tag) in order to output my text. This small bit of code looks confusing and can get lost inside larger blocks of server-side code.

Instead, I can use an undocumented hack. Razor helpers are implemented by compiling a lambda expression as an Action<TextWriter>. The lambda expression receives a TextWriter parameter named __razor_helper_writer. (You can see this by writing a Razor page with a compilation error and clicking Show Complete Compilation Source) There is nothing preventing me from using this parameter yourself. (it even shows up in IntelliSense!)

This can be called the same way as the helper. Instead of using the special helper support, the call will just print a string, the same you print any other string. Therefore, the function’s output will be HTML-escaped. To prevent that, you can change the function to return an HtmlString.