This project is read-only.

Breaking change with templates?

Mar 3, 2015 at 8:54 PM
I've returned to a project that was using Razor Generator templates in a (simple, non-MVC) class library and am finding that regenerating the code to any of cshtml yields broken code.

I create a fresh console app, add the following content for a file called Page.cshtml and hit CTRL+S:
@* Generator: Template *@
@* Namespace: Foo *@
<!DOCTYPE html>
<html lang="en" xmlns="">
    <meta charset="utf-8" />
The generated code I got was this:
#pragma warning disable 1591
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.34014
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>

namespace ASP
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    [System.CodeDom.Compiler.GeneratedCodeAttribute("RazorGenerator", "")]
    public partial class _Page_cshtml : RazorGenerator.Templating.RazorTemplateBase
#line hidden

        public override void Execute()


WriteLiteral("\r\n<!DOCTYPE html>\r\n<html lang=\"en\" xmlns=\"\">\r\n<head>\r" +
"\n    <meta charset=\"utf-8\" />\r\n    <title></title>\r\n</head>\r\n<body>\r\n</body>\r\n</" +

#pragma warning restore 1591
I've tried to directives like Namespace to change the ASP namespace (this was not needed before) and GeneratePrettyNames to get a clean class name but none of the directives seem to be helping.

I returned to my project after quite some months so wonder if there's been a breaking change in Razor Generator with more recent versions?

Mar 3, 2015 at 9:13 PM
Yes, there was a breaking change. Try something like this. It's part of a working sample, which you can use as an example.
Marked as answer by atifaziz on 3/3/2015 at 1:49 PM
Mar 3, 2015 at 9:48 PM
Thanks, David, that fixed it!

So if I understand, putting the directives on separate lines like so:
@* Generator: Template *@
@* GeneratePrettyNames: true *@
…is not the right syntax?
Mar 3, 2015 at 9:51 PM
No, I guess they all have to be on one line. Pranav can confirm.
Mar 3, 2015 at 10:00 PM
If that's case (pending Pranav's confirmation) then it may be a good idea to update the directives examples on the home page to avoid misleading others. I was copying and pasting the various directives to avoid typo and to see which ones were needed to restore the old default for templates, but none of them were working. The fact that each was wrapped between @* and *@ gave the impression that they could be put on separate lines for clarity. Thanks for sorting me out meanwhile and so quickly.
Mar 3, 2015 at 10:12 PM
Thanks for the suggestion. I'm clarified the doc.
Mar 3, 2015 at 10:49 PM
So studying the directives parser code, it looks like it only takes the first Razor comment in the file (from first @*, whitespace leading allowed, token to first *@) and parses directives within as key-colon-value pairs. This explains why putting each directive in its own comment or line is no good.
Mar 3, 2015 at 11:02 PM
Yes, clearly that's what's going on. And yes, error handling could be better :)
Mar 3, 2015 at 11:18 PM
:) …and just to help for the documentation…

Incidentally, directives can be on separate lines when placed in a razorgenerator.directives file. This is the route I've finally taken; adding a global file at the project level. It enabled to leave all CSHTML files intact while addressing the breaking change centrally.

In a CSHTML file, directives can be on separate lines too as long as they are part of the first comment, e.g.:
@* Generator: Template
   GeneratePrettyNames: true *@