2016-08-12 44 views
0

我們正在嘗試使用RazorEngine和Webapi生成模板。如果我在cshtml代碼中包含部分。拋出異常。有沒有不同的方式來做到這一點。@section with RazorEngine

類節目 { 靜態無效的主要(字符串[]參數) { 變種配置=新TemplateServiceConfiguration { 調試=真,
CachingProvider =新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 string Name { get; set; } 
    public string Title { get; set; } 
    public List<string> Books { get; set; } 

}

錯誤顯示的

錯誤在編譯模板。 請嘗試以下來解決這種情況: *如果問題是關於缺失/無效引用或多重定義,請嘗試手動加載 缺少的引用(在編譯應用程序域!)或 通過提供自己的手動指定引用IReferenceResolver實現。 詳情請參閱https://antaris.github.io/RazorEngine/ReferenceResolver.html。 當前所有參考文獻必須以文件形式提供! *如果'class'不包含'member'的定義: 嘗試使用另一個modelType(例如'null'使模型動態化)。 注意:不能使用typeof(動態)使模型動態化! 或嘗試使用靜態而不是匿名/動態類型。 有關錯誤的更多詳細信息: - 錯誤:(49,24)字符文字中的字符太多 可以在(請刪除文件夾)中找到編輯的臨時文件:C:\ Users \ madhavi \ AppData \ Local \ TEMP \ RazorEngine_oeduzlw2.nyw 我們試圖編譯模板是: ------------- ----------- 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 -----------

生成的源代碼是:

------------- 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

加載的程序集列表: C:\ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ mscorlib.dll 加載程序集:C:\ WINDOWS \ assembly \ GAC_MSIL \ Microsoft.VisualStudio.HostingProcess.Utilities \ 14.0.0.0__b03f5f7f11d50a3a \ Microsoft.VisualStudio。 HostingProcess.Utilities.dll 加載的程序集:C:\ WINDOWS \ Microsoft.Net \ assembly \ GAC_MSIL \ System.Windows.Forms \ v4.0_4.0.0.0__b77a5c561934e089 \ System.Windows.Forms.dll 加載的程序集:C:\ WINDOWS \ Microsoft.Net \裝配\ GAC_MSIL \ SYSTEM \ v4.0_4.0.0.0__b77a5c561934e08 9 \ System.dll 加載程序集:C:\ WINDOWS \ Microsoft.Net \ assembly \ GAC_MSIL \ System.Drawing \ v4.0_4.0.0.0__b03f5f7f11d50a3a \ System.Drawing.dll 加載程序集:C:\ WINDOWS \ assembly \ GAC_MSIL \ Microsoft.VisualStudio.HostingProcess.Utilities。Sync \ 14.0.0.0__b03f5f7f11d50a3a \ Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll 加載的程序集:C:\ WINDOWS \ assembly \ GAC_MSIL \ Microsoft.VisualStudio.Debugger.Runtime \ 14.0.0.0__b03f5f7f11d50a3a \ Microsoft.VisualStudio.Debugger。 Runtime.dll 加載程序集:C:\ Users \ madhavi \ Documents \ Visual Studio 2015 \ TestIntegrationProject \ EmailTemplateGeneration \ EmailTemplateGeneration \ bin \ Debug \ EmailTemplateGeneration.vshost.exe 加載程序集:C:\ WINDOWS \ Microsoft.Net \ assembly \ GAC_MSIL \ System.Core \ v4.0_4.0.0.0__b77a5c561934e089 \ System.Core.dll 加載程序集:C:\ WINDOWS \ Microsoft.Net \ assembly \ GAC_MSIL \ System.Xml.Linq \ v4.0_4.0.0.0__b77a5c561934e089 \ System.Xml.Linq.dll 加載的程序集:C:\ WINDOWS \ Microsoft.Net \ assembly \ GAC_MSIL \ System.Data.DataSetExtensions \ v4.0_4.0.0.0__b77a5c561934e089 \ System.Data.DataSetExtensions.dll加載程序集:C:\ WINDOWS \ Microsoft.Net \程序集\ GAC_MSIL \ Microsoft.CSharp \ v4.0_4.0.0.0__b03f5f7f11d50a3a \ Microsoft.CSharp.dll 加載程序集:C:\ WINDOWS \ Microsoft.Net \ assembly \ GAC_32 \ System.Data \ v4.0_4.0.0.0__b77a5c561934e089 \ System.Data.dll 加載程序集:C:\ WINDOWS \ Microsoft.Net \ assembly \ GAC_MSIL \ System.Net.Http \ v4.0_4.0.0.0__b03f5f7f11d50a3a \ System .Net.Http.dll 加載程序集:C:\ WINDOWS \ Microsoft.Net \ assembly \ GAC_MSIL \ System.Xml \ v4.0_4.0.0.0__b77a5c561934e089 \ System.Xml.dll 加載程序集:C:\ Users \ madhavi \ Documents \ Visual Studio 2015 \ TestIntegrationProject \ EmailTemplateGeneration \ EmailTemplateGeneration \ bin \ Debug \ EmailTemplateGeneration.exe 加載的程序集:C:\ Users \ madhavi \ Documents \ Visual Studio 2015 \ TestIntegrationProject \ EmailTemplateGeneration \ EmailTemplateGeneration \ bin \ Debug \ Razor Engine.dll 加載程序集:C:\ WINDOWS \ Microsoft.Net \程序集\ GAC_MSIL \ System.Configuration \ v4.0_4.0.0.0__b03f5f7f11d50a3a \ System.Configuration.dll 加載程序集:C:\ Users \ madhavi \ Documents \加載程序集:C:\ WINDOWS \ Microsoft.Net \ assembly \ GAC_MSIL \ System.Dynamic \ v4.0_4.0.0.0__b03f5f7f11d50a3a \ Visual Studio 2015 \ System.Dynamic.dll

回答