2016-08-12 2 views
0

私たちはRazorEngineとWebapiを使ってテンプレートを生成しようとしています。私はcshtmlコードのセクションを含める場合。例外がスローされます。これを行う別の方法がありますか?@ section with RazorEngine

クラスプログラム { 静的な無効メイン(文字列[] args)=新しいTemplateServiceConfiguration { デバッグ=真、
CachingProvider =新しいDefaultCachingProvider(T => {}) } { VARの設定。テンプレートをコンパイルしながら

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

}

エラー

エラーに示します。 *見つからない/無効な参照または複数の定義に関する問題がある場合は、 を手動で(コンパイルするappdomain!)または を手動でロードしてくださいIReferenceResolverの実装。 詳細はhttps://antaris.github.io/RazorEngine/ReferenceResolver.htmlを参照してください。 現在、すべての参照はファイルとして利用可能である必要があります。 * 'class'に 'member'の定義が含まれていない場合: 別のmodelTypeを試してください(たとえば、モデルを動的にするには 'null')。 注:typeof(dynamic)を使用してモデルを動的にすることはできません! または、匿名/動的型の代わりに静的型を使用してください。 エラーの詳細: - エラー:(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 アセンブリがロードされました。 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 \ 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 \ System.Dynamic.dll

答えて

0

あなたは見つけるでしょう、関連compiler errorを検索:Why I'm getting CS1012: "Too many characters in character literal" and CS0019?

この情報の後、あなたがもう一度あなたのソースを見て、エラーを検出することができます。

あなたは、C#で文字列の "の代わり 'を使用する必要があります
@RenderSection('section1',false) 

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

に変換されます。

関連する問題