2013-07-24 2 views
5

私はRazorEngine(http://razorengine.codeplex.com/)でHTML文書を生成しようとしています。すべてがで、ほとんどがですが、私は今、HTMLの一部が正しくレンダリングされており、その中に入れ子になっているHTMLが、テーブルを表示するブラウザではなく、リテラルHTMLとしてレンダリングされています。& divs 、それは例えば表示しているHTMLがRazorEngineを使用してリテラル文字列としてレンダリングされています。どうすればこれを防ぐことができますか?

"<table></table><div></div>" 

私は、次を呼び出すことによって、このプロセスをキックオフ:

string completeHTML = RazorEngine.Razor.Parse("InstallationTemplate.cshtml", new { Data = viewModels }); 

completeHTMLは、ファイルに書き込まれます。

"InstallationTemplate.cshtml" は次のように定義される。

次のように InstallationReportDigiChannelsが定義されている
@{ 
    var installationReport = new InstallationReport(Model.Data); 
} 

<!DOCTYPE html> 
<html> 
    <head></head> 
    <body>   
     <div> 
      <!-- I would expect this to write the rendered HTML 
       in place of "@installationReport.DigiChannels()" --> 
      @installationReport.DigiChannels()  
     </div> 
    </body> 
</html> 

public static class InstallationReportExtensions 
{ 
    public static string DigiChannels(this InstallationReport installationReport) 
    { 
     return installationReport.GetDigiChannelsHtml(); 
    } 
} 

public class InstallationReport 
{ 
    public string GetDigiChannelsHtml() 
    { 
     // the following renders the template correctly 
     string renderedHtml = RazorReport.GetHtml("DigiChannels.cshtml", GetDigiChannelData()); 
     return renderedHtml; 
    } 
} 

public static string GetHtml(string templateName, object data) 
{ 
    var templateString = GetTemplateString(templateName); 

    return RazorEngine.Razor.Parse(templateString, data); 
} 

GetDigiChannelsHtml()後に実行戻るrenderedHtml、実行戻りのラインTemplateBase.csに方法ITemplate.Run(ExecuteContext context)に定義され、

string ITemplate.Run(ExecuteContext context) 
    { 
     _context = context; 

     var builder = new StringBuilder(); 
     using (var writer = new StringWriter(builder)) 
     { 
      _context.CurrentWriter = writer; 

      Execute(); // this is where my stuff gets called 

      _context.CurrentWriter = null; 
     } 

     if (Layout != null) 
     { 
      // Get the layout template. 
      var layout = ResolveLayout(Layout); 

      // Push the current body instance onto the stack for later execution. 
      var body = new TemplateWriter(tw => tw.Write(builder.ToString())); 
      context.PushBody(body); 

      return layout.Run(context); 
     } 

     return builder.ToString(); 
    } 

builder.ToString()を調べると、InstallationTemplate.cshtmlのものには適切なHTMLが含まれており、DigiChannels.cshtmlにはHTMLがエスケープされていることがわかります。たとえば:

enter image description here

にはどうすれば@installationReport.DigiChannels()ではなく、それが現在やっているエスケープHTMLの正しいHTMLが含まれるように得ることができますか?

答えて

18

は、あなたが試してみました:

@Raw(installationReport.DigiChannels()) 

編集を:私は@Rawに以下の方法(MVC3)

@Html.Raw(installationReport.DigiChannels()) 
+0

いいえ、私はそれを試していませんでした(知らなかった!)。それは完璧に働いた。ありがとう。あなたの答えを正しいものにする前に、数分待たなければなりません。 – DaveDev

+0

ありがとうございます。はい、Html.Rawと同じことをします(ASP.NET MVCでカミソリを使用する場合) –

5

に代替を、それを使用することができ、適切な場所でHtmlString秒を返すために、あなたのAPIを変更することです

再度エンコードするべきではないHTMLエンコードされた文字列を表します。

デフォルトのカミソリの動作は、stringをエンコードします。

関連する問題