View 如何在MVC6中定义具有默认内容的节?

View 如何在MVC6中定义具有默认内容的节?,view,migration,asp.net-core,extension-methods,asp.net-core-mvc,View,Migration,Asp.net Core,Extension Methods,Asp.net Core Mvc,我目前正在尝试将ASP.net MVC 5项目迁移到MVC 6 如何迁移以下代码: public static class SectionExtensions { public static HelperResult RenderSection(this WebPageBase webPage, [RazorSection] string name, Func<dynamic, HelperResult> defaultContents) { retu

我目前正在尝试将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);
    }
}
公共静态类扩展
{
public static HelperResult RenderSection(此网页库网页,[RazorSection]字符串名称,Func defaultContents)
{
返回webPage.IsSectionDefined(名称)?webPage.RenderSection(名称):defaultContents(空);
}
}

[RazorSection]是JetBrains.Annotations程序集的一部分

我在
Microsoft.AspNet.Mvc.Razor

namespace Stackoverflow
{
    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);
        }
    }
}
另一种方法是只在视图中执行此操作(我更喜欢这种方法,似乎简单得多):

您不必包含
static
关键字,但在调用它时,您必须引用命名空间中的实际类,并将“this”传递到函数中

@SectionExtensions.RenderSection(this, "extra", @<span>This is the default!</span>)
然后是剃须刀页面:

section test {
    @this.RenderSection("extra", new HtmlString("<span>this is the default!</span>")); 
}
section测试{
@RenderSection(“额外”,新的HtmlString(“这是默认值!”);
}

感谢您的回复。我不喜欢另一种方式,只是因为我喜欢将if语句保留在视图之外。我尝试了您提到的方法,除了名称空间之外,其他一切似乎都正常。似乎我必须使用:SectionExtensions.RenderSection而不是this.RenderSection。using语句似乎没有任何作用?我已经更新了我的答案,因为某些原因,我以前的编辑没有通过。要直接引用静态类,必须使用static关键字,例如'@using static Stackoverflow.SessionExtensions',但是,如果只引用名称空间,则不必添加静态关键字,例如'@using Stackoverflow'。这完全取决于是否要将整个名称空间包含到视图中。在这种情况下,您必须调用'@SessionExtension.RenderSection',而不是'@this.RenderSection',然后将'this'传递到函数RenderSection(this,“test”,@default)。如果有帮助的话,请告诉我。我原以为这是有效的,但我遇到了一个问题。假设您有一个嵌套布局。在主布局中有一个标准的@RenderSection(“test”,false)。然后,您就有了继承主布局的SubMasterLayout。如果我尝试@section test{@SectionExtensions.RenderSection(这是“额外的”,“这是默认的!)),我会得到以下异常:“\uuRazor\uTemplate\uWriter”:参数或局部变量不能与方法类型参数同名。这在MVC5中非常有效?这是razor中的一个bug。基本上,当调用@section test{}时,它会创建一个参数方法“\uu razor\u template\u writer”,然后当您尝试创建一个razor模板委托“@test!”在该部分中,它创建了另一个同名的参数方法,因此出现了bug。解决方法可以是@SectionExtensions.RenderSection(“hello”,item=>newhtmlString(“test”))。[..][..]或者您可以从RenderSection方法中删除Func,只需将其设为IHtmlContent,并使用@SectionExtensions.RenderSection(“hello”,新的HtmlString(“test”)调用它即可。这样,您将得到相同的结果,只是不需要调用模板“@

”。请参阅我已记录了该问题。如果您在解决方法方面有任何问题,请告诉我。
@using Stackoverflow
@SectionExtensions.RenderSection(this, "extra", @<span>This is the default!</span>)
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>")); 
}