asp.NetテンプレートエンジンRazor呼び出し外部メソッド使用例
3838 ワード
本文の例はaspを述べた.NetテンプレートエンジンRazorは外部メソッドの使い方を呼び出します.皆さんの参考にしてください.具体的には以下の通りです.
まずRazorを使用するには、cshtmlを読み込み、cshtmlを解析しながらcacheNameを指定します.
このステップは、DRYの原則に従うために、このコードをRazorHelper()メソッドにカプセル化する繰り返しです.
cshtmlでRazorで外部メソッドを呼び出す方法
1.まずcshtmlファイルでtest 1とtest 2が存在するクラスのネーミングスペースを参照する
まずRazorを使用するには、cshtmlを読み込み、cshtmlを解析しながらcacheNameを指定します.
このステップは、DRYの原則に従うために、このコードをRazorHelper()メソッドにカプセル化する繰り返しです.
public class RazorHelper
{
public static string ParseRazor(HttpContext context, string csHtmlVirtualPath, object model)
{
string fullPath = context.Server.MapPath(csHtmlVirtualPath);
string cshtml = File.ReadAllText(fullPath);
string cacheName = fullPath + File.GetLastWriteTime(fullPath);
string html = Razor.Parse(cshtml,model,cacheName);
return html;
}
}
cshtmlでRazorで外部メソッドを呼び出す方法
1.まずcshtmlファイルでtest 1とtest 2が存在するクラスのネーミングスペースを参照する
@using WebTest1.RazorDemo;
@RazorTest.test1()<br/>
@RazorTest.test2()
</code></pre>
</div>
<p>2. RazorHelper.ParseRazor(), cshtml </p>
<div class="jb51code">
<pre><code>
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string html = RazorHelper.ParseRazor(context, @"~/Razordemo/Razor2.cshtml", null);
context.Response.Write(html);
}
</code></pre>
</div>
<p><strong> cshtml ?</strong></p>
<p> , cshtml checkbox </p>
<p>1. </p>
<div class="jb51code">
<pre><code>
bool gender = true;
string html = RazorHelper.ParseRazor(context, @"~/Razordemo/Razor2.cshtml", new { Gender = gender });
</code></pre>
</div>
<p>2. cshtml checkbox checked </p>
<p><input type="checkbox"/><br/> <!-- , Model --></p>
<p> ? 。</p>
<p> , cshtml 。</p>
<p><strong> :</strong></p>
<p> cshtml checkbox。</p>
<p>1. CheckBox() </p>
<div class="jb51code">
<pre><code>
public static RawString CheckBox(string name, string id, bool isChecked)
{
StringBuilder sb = new StringBuilder();
sb.Append("<input type="checkbox" id="").Append(id).Append("" if="" sb.append=""/>");
return new RawString(sb.ToString());
}
</code></pre>
</div>
<p>2. cshtml </p>
<div class="jb51code">
<pre><code>
string html = RazorHelper.ParseRazor(context, @"~/Razordemo/Razor2.cshtml", null);
context.Response.Write(html);
</code></pre>
</div>
<p>3. cshtml CheckBox() , checkbox cshtml</p>
<div class="jb51code">
<pre><code>
@using WebTest1.RazorDemo;<!--test1 test2 -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title/>
@RazorTest.CheckBox("apple","apple",true)
</code></pre>
</div>
<p> asp.net 。</p>
<div class="clearfix">
<span id="art_bot" class="jbTestPos"/>
</div>
</div>
</div>
</div>