2016-08-12 8 views
0

우리는 RazorEngine과 Webapi를 사용하여 템플릿을 생성하려고합니다. cshtml 코드에 section을 포함하면. 예외가 발생합니다. 이 작업을 수행하는 다른 방법이 있습니까?@ section with RazorEngine

클래스 프로그램 { 정적 무효 메인 (문자열 []에 args) { VAR의 구성 = 새로운 TemplateServiceConfiguration { 디버그 = TRUE,
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); 
    } 
} 

공용 클래스 BookList {

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

}

오류가

오류가 표시됨. 상황을 해결하려면 다음을 시도하십시오. * 누락/잘못된 참조 또는 여러 정의에 대한 문제인 경우 누락 된 참조를 수동으로 (컴파일하는 appdomain에서) 로드하거나 직접 입력하여 참조를 직접 지정하십시오. IReferenceResolver 구현입니다. 자세한 내용은 https://antaris.github.io/RazorEngine/ReferenceResolver.html을 참조하십시오. 현재 모든 참조는 파일로 제공되어야합니다! * '클래스'에 '회원'에 대한 정의가 포함되어 있지 않은 경우 : 다른 modelType을 시도하십시오 (예 : 모델을 동적으로 만들려면 'null'). 참고 : 모델을 동적으로 만들기 위해 typeof (동적)을 사용할 수 없습니다! 또는 익명/동적 유형 대신 정적 사용을 시도하십시오. 오류에 대한 자세한 내용 : - 오류 : (49, 24) 문자가 너무 많음 문자 임시 파일을 폴더에서 삭제하십시오 (C : \ Users \ madhavi \ AppData \ Local \ 임시 \의 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 \ 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 \ assembly \ 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 : \ WINDOWS \ Microsoft.Net \ assembly \ Visual Studio 2015 \ TestIntegrationProject \ EmailTemplateGeneration \ EmailTemplateGeneration \ bin \ Debug \ EmailTemplateGeneration.vshost.exe 로드 된 어셈블리 : C : \ Users \ madhavi \ C : \ WINDOWS \ Microsoft.Net \ assembly \ GAC_MSIL \ System.Xml.Linq \ v4.0_4.0.0.0__b77a5c561934e089 \ GAC_MSIL \ 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 \ assembly \ 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 \ assembly \ GAC_MSIL \ System.Configuration \ v4.0_4.0.0.0__b03f5f7f11d50a3a \ System.Configuration.dll 로드 된 어셈블리 : C : \ Users \ madhavi \ Documents \ Visual Studio 2015 \ TestIntegrationProject \ EmailTemplateGeneration \ EmailTemplateGeneration \ bin \ Debug \ System.Web.Razor.dll 로드 된 어셈블리 : C : \ WINDOWS \ Microsoft.Net \ assembly \ GAC_MSIL \ System.Dynamic \ v4.0_4.0.0.0__b03f5f7f11d50a3a \ 관련 compiler error를 검색 할 때 System.Dynamic.dll

답변