高级 Razor : Templated Razor Delegate

来源:互联网 发布:ubuntu wireshark lua 编辑:程序博客网 时间:2024/05/29 18:54


1. 使用 Razor 模板委托 是 Fun<T,HelperResult> 类型,在模板中( Func<dynamic, object> strongTemplate =@<strong>@item</strong>;
)@item 是个神奇的参数,尽管这些委托只能有这样的参数,但是模板可以多次引用。

<div>    @{        Func<dynamic, object> strongTemplate =@<strong>@item</strong>;    }    <p>Advance Razor(Templated Razor Delegate) By zhou44129879</p>    @strongTemplate("This is bolded.")</div>

2. 我们可以扩展 WebPageBase 类中的 RenderSection 方法来使用Fun<T,HelperResult> 类型.

public static class RazorLayoutHelpers {public static HelperResult RenderSection(this WebPageBase webPage,string name,Func<dynamic, HelperResult> defaultContents) {if (webPage.IsSectionDefined(name)) {return webPage.RenderSection(name);}return defaultContents(null);}}

调用:

<footer>@this.RenderSection("Footer", @<span>This is the default.</span>)</footer>

扩展发放是.net 3.5的新特性,不懂得可以自己网上看下(this WebPageBase webPage)。




原创粉丝点击