Madhavi Gunturi
Madhavi Gunturi

Reputation: 33

@section with RazorEngine

We are trying to generate templates using RazorEngine and Webapi. If i include section in the cshtml code. Exception is thrown. Is there a different way of doing this.

class Program { static void Main(string[] args) { var config = new TemplateServiceConfiguration { Debug = true,
CachingProvider = new DefaultCachingProvider(t => { }) };

        var service = RazorEngineService.Create(config);

        Engine.Razor = service;
        service.AddTemplate("layout", @"<!DOCTYPE html> 


    <html> <head>
                                <style>
         body
         {
             font-size: .85em;
             font-family: 'Trebuchet MS' , Verdana, Helvetica, Sans-Serif;
             color: #232323;
             background-color: #fff;
         }

         header, footer, nav, section
         {
             display: block;
         }
     </style>
     <title>@Model.Name</title>
 </head>
 <body>
     <div>
        @RenderSection('section1',false)
     </div>
    <div id='content'>
        @RenderBody()
   </div>   
 </body>
 </html>");

        service.AddTemplate("template", @"@{Layout = ""layout"";} 
<h3>@Model.Title</h3>
           @section section1{
                <text>sample content</text>}");

        var bookList = new BookList()
        {
            Name = "Madhavi",
            Title = "Top 10 books",
            Books = new List<string>
            {
                "Harry poter1",
                "Harry poter2",
                "Harry poter3",
                "The hunger game1",
                "Harry poter1",
                "The Hobbit",
                "Eat Pray Love",
                "A Tale of Two Cities",
                "The Lion, the Witch and the Wardrobe",
                "Think and Grow Rich"
            }
        };

        service.Compile("template", bookList.GetType());
        var result = service.Run("template", bookList.GetType(), bookList);
        Console.WriteLine("Result is: {0}", result);
    }
}

public class BookList {

    public string Name { get; set; }
    public string Title { get; set; }
    public List<string> Books { get; set; }

}

Error Shown

Errors while compiling a Template. Please try the following to solve the situation: * If the problem is about missing/invalid references or multiple defines either try to load the missing references manually (in the compiling appdomain!) or Specify your references manually by providing your own IReferenceResolver implementation. See https://antaris.github.io/RazorEngine/ReferenceResolver.html for details. Currently all references have to be available as files! * If you get 'class' does not contain a definition for 'member': try another modelType (for example 'null' to make the model dynamic). NOTE: You CANNOT use typeof(dynamic) to make the model dynamic! Or try to use static instead of anonymous/dynamic types. More details about the error: - error: (49, 24) Too many characters in character literal Temporary files of the compilation can be found in (please delete the folder): C:\Users\madhavi\AppData\Local\Temp\RazorEngine_oeduzlw2.nyw The template we tried to compile is: ------------- START -----------

<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            font-size: .85em;
            font-family: 'Trebuchet MS', Verdana, Helvetica, Sans-Serif;
            color: #232323;
            background-color: #fff;
        }

        header, footer, nav, section {
            display: block;
        }
    </style>
    <title>@Model.Name</title>
</head>
<body>
    <div>
        @RenderSection('section1',false)
    </div>
    <div id='content'>
        @RenderBody()
    </div>
</body>
</html>

------------- END -----------

The generated source code is:

------------- START -----------

//------------------------------------------------------------------------------
// <auto-generated>
    //     This code was generated by a tool.
    //     Runtime Version:4.0.30319.42000
    //
    //     Changes to this file may cause incorrect behavior and will be lost if
    //     the code is regenerated.
    //
</auto-generated>
//------------------------------------------------------------------------------

namespace CompiledRazorTemplates.Dynamic {
    using System;
    using System.Collections.Generic;
    using System.Linq;


    public class RazorEngine_090e814295334051a2dbe5f2a700d613 : RazorEngine.Templating.TemplateBase<EmailTemplateGeneration.BookList>
    {



    public RazorEngine_090e814295334051a2dbe5f2a700d613() {
    }

    public override void Execute() {
    WriteLiteral(@"<!DOCTYPE html>
    <html>
    <head>
        <style>
            body {
                font-size: .85em;
                font-family: 'Trebuchet MS', Verdana, Helvetica, Sans-Serif;
                color: #232323;
                background-color: #fff;
            }

            header, footer, nav, section {
                display: block;
            }
        </style>
        <title>
            ");

            Write(Model.Name);

            WriteLiteral("
        </title>\r
    </head>\r
    <body>
        \r     <div>
            \r");

            WriteLiteral("        ");

            Write(RenderSection('section1',false));

            WriteLiteral("\r
        </div>\r    <div"); WriteLiteral(" id=\'content\'");
                            WriteLiteral(">
            \r");

            WriteLiteral("        ");

            Write(RenderBody());

            WriteLiteral("\r   </div>   \r
    </body>\r
</html>");

        }
    }
}

------------- END -----------

List of loaded Assemblies: C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll Loaded Assembly: C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\14.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll Loaded Assembly: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll Loaded Assembly: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll Loaded Assembly: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll Loaded Assembly: C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync\14.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll Loaded Assembly: C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\14.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll Loaded Assembly: C:\Users\madhavi\Documents\Visual Studio 2015\TestIntegrationProject\EmailTemplateGeneration\EmailTemplateGeneration\bin\Debug\EmailTemplateGeneration.vshost.exe Loaded Assembly: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll Loaded Assembly: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xml.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.Linq.dll Loaded Assembly: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Data.DataSetExtensions\v4.0_4.0.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll Loaded Assembly: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.CSharp\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.CSharp.dll Loaded Assembly: C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll Loaded Assembly: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Net.Http\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Net.Http.dll Loaded Assembly: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll Loaded Assembly: C:\Users\madhavi\Documents\Visual Studio 2015\TestIntegrationProject\EmailTemplateGeneration\EmailTemplateGeneration\bin\Debug\EmailTemplateGeneration.exe Loaded Assembly: C:\Users\madhavi\Documents\Visual Studio 2015\TestIntegrationProject\EmailTemplateGeneration\EmailTemplateGeneration\bin\Debug\RazorEngine.dll Loaded Assembly: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll Loaded Assembly: C:\Users\madhavi\Documents\Visual Studio 2015\TestIntegrationProject\EmailTemplateGeneration\EmailTemplateGeneration\bin\Debug\System.Web.Razor.dll Loaded Assembly: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Dynamic\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Dynamic.dll

Upvotes: 0

Views: 773

Answers (1)

matthid
matthid

Reputation: 1704

When searching for the relevant compiler error you will find: Why I'm getting CS1012: "Too many characters in character literal" and CS0019?

After this info you can look at your source again and detect the error:

    @RenderSection('section1',false)

Is translated to

        Write(RenderSection('section1',false));

You should use " for strings instead of ' in C#.

Upvotes: 0

Related Questions