如何在 MVC 6 中定义具有默认内容的部分?
How can you define sections with default content in MVC 6?
我目前正在尝试将 ASP.net MVC 5 项目迁移到 MVC 6。
我将如何迁移以下代码:
public static class SectionExtensions
{
public static HelperResult RenderSection(this WebPageBase webPage, [RazorSection] string name, Func<dynamic, HelperResult> defaultContents)
{
return webPage.IsSectionDefined(name) ? webPage.RenderSection(name) : defaultContents(null);
}
}
[RazorSection] 是 JetBrains.Annotations 程序集的一部分。
我在 Microsoft.AspNet.Mvc.Razor
中使用 RazorPage
而不是 WebPageBase
namespace Whosebug
{
public static class SectionExtensions
{
public static IHtmlContent RenderSection(this RazorPage page, [RazorSection] string name, Func<dynamic, IHtmlContent> defaultContents)
{
return page.IsSectionDefined(name) ? page.RenderSection(name) : defaultContents(null);
}
}
}
然后引用razor页面@using static Whosebug.SessionExtensions
中的class,这样调用:
@this.RenderSection("extra", @<span>This is the default!</span>))
另一种替代方法是在视图中执行此操作(我更喜欢这种方法,看起来简单多了):
@if (IsSectionDefined("extra"))
{
@RenderSection("extra", required: false)
}
else
{
<span>This is the default!</span>
}
希望对您有所帮助。
更新 1(来自评论)
通过引用命名空间
@using Whosebug
您不必包含 static
关键字,但在调用它时,您必须在命名空间中引用实际的 class 并传递 'this ' 进入函数。
@SectionExtensions.RenderSection(this, "extra", @<span>This is the default!</span>)
更新 2
razor 中存在一个错误,不允许您在一个部分中调用模板委托 Func <dynamic, object> e = @<span>@item</span>;
。请参阅 https://github.com/aspnet/Razor/issues/672
当前的解决方法:
public static class SectionExtensions
{
public static IHtmlContent RenderSection(this RazorPage page, [RazorSection] string name, IHtmlContent defaultContents)
{
return page.IsSectionDefined(name) ? page.RenderSection(name) : defaultContents;
}
}
然后是剃刀页面:
section test {
@this.RenderSection("extra", new HtmlString("<span>this is the default!</span>"));
}
我目前正在尝试将 ASP.net MVC 5 项目迁移到 MVC 6。
我将如何迁移以下代码:
public static class SectionExtensions
{
public static HelperResult RenderSection(this WebPageBase webPage, [RazorSection] string name, Func<dynamic, HelperResult> defaultContents)
{
return webPage.IsSectionDefined(name) ? webPage.RenderSection(name) : defaultContents(null);
}
}
[RazorSection] 是 JetBrains.Annotations 程序集的一部分。
我在 Microsoft.AspNet.Mvc.Razor
RazorPage
而不是 WebPageBase
namespace Whosebug
{
public static class SectionExtensions
{
public static IHtmlContent RenderSection(this RazorPage page, [RazorSection] string name, Func<dynamic, IHtmlContent> defaultContents)
{
return page.IsSectionDefined(name) ? page.RenderSection(name) : defaultContents(null);
}
}
}
然后引用razor页面@using static Whosebug.SessionExtensions
中的class,这样调用:
@this.RenderSection("extra", @<span>This is the default!</span>))
另一种替代方法是在视图中执行此操作(我更喜欢这种方法,看起来简单多了):
@if (IsSectionDefined("extra"))
{
@RenderSection("extra", required: false)
}
else
{
<span>This is the default!</span>
}
希望对您有所帮助。
更新 1(来自评论)
通过引用命名空间
@using Whosebug
您不必包含 static
关键字,但在调用它时,您必须在命名空间中引用实际的 class 并传递 'this ' 进入函数。
@SectionExtensions.RenderSection(this, "extra", @<span>This is the default!</span>)
更新 2
razor 中存在一个错误,不允许您在一个部分中调用模板委托 Func <dynamic, object> e = @<span>@item</span>;
。请参阅 https://github.com/aspnet/Razor/issues/672
当前的解决方法:
public static class SectionExtensions
{
public static IHtmlContent RenderSection(this RazorPage page, [RazorSection] string name, IHtmlContent defaultContents)
{
return page.IsSectionDefined(name) ? page.RenderSection(name) : defaultContents;
}
}
然后是剃刀页面:
section test {
@this.RenderSection("extra", new HtmlString("<span>this is the default!</span>"));
}