首页 > 编程语言 >What is @RenderSection in asp.net MVC - Stack Overflow

What is @RenderSection in asp.net MVC - Stack Overflow

时间:2023-10-16 15:26:27浏览次数:50  
标签:RenderSection What asp false cshtml required scripts page

What is @RenderSection in asp.net MVC - Stack Overflow

What is the purpose of @RenderSection and how does it function? I understand what bundles do, but I have yet to figure out what this does and it's probably important.

@RenderSection("scripts", required: false)

Perhaps a small example on how to use it?

 

回答1

If you have a _Layout.cshtml view like this

<html>
    <body>
        @RenderBody()
        @RenderSection("scripts", required: false)
    </body>
</html>

then you can have an index.cshtml content view like this

@section scripts {
     <script type="text/javascript">alert('hello');</script>
}

the required indicates whether or not the view using the layout page must have a scripts section

 

回答2

Supposing that:

1. You have a _Layout.cshtml view like this.

<html>
    <body>
        @RenderBody()
       
    </body>
    <script type="text/javascript" src="~/lib/layout.js"></script>
    @RenderSection("scripts", required: false)
</html>

2. You have Contacts.cshtml.

@section Scripts{
    <script type="text/javascript" src="~/lib/contacts.js"></script>

}
<div class="row">
    <div class="col-md-6 col-md-offset-3">
        <h2>    Contacts</h2>
    </div>
</div>

3. You have About.cshtml.

<div class="row">
    <div class="col-md-6 col-md-offset-3">
        <h2>    Contacts</h2>
    </div>
</div>

On your layout page, if required is set to false: @RenderSection("scripts", required: false), when the page renders and the user is on the about page, contacts.js won't render.

    <html>
        <body><div>About<div>             
        </body>
        <script type="text/javascript" src="~/lib/layout.js"></script>
    </html>

If required is set to true: @RenderSection("scripts", required: true), When page renders and user is on the About page, contacts.js still gets rendered.

<html>
    <body><div>About<div>             
    </body>
    <script type="text/javascript" src="~/lib/layout.js"></script>
    <script type="text/javascript" src="~/lib/contacts.js"></script>
</html>

IN SHORT, when set to true, whether you need it or not on other pages, it will get rendered anyhow. If set to false, it will render only when the child page is rendered.

 

标签:RenderSection,What,asp,false,cshtml,required,scripts,page
From: https://www.cnblogs.com/chucklu/p/17767358.html

相关文章

  • Build ASP.NET Core applications deployed as Linux containers into an AKS/Kuberne
    原文:https://learn.microsoft.com/en-us/dotnet/architecture/containerized-lifecycle/design-develop-containerized-apps/build-aspnet-core-applications-linux-containers-aks-kubernetesAzureKubernetesServices(AKS)isAzure'smanagedKubernetesorchestrat......
  • ASP.NET Core Minimal API之optional route parameter with default value and option
    publicstaticvoidMain(string[]args){varbuilder=WebApplication.CreateBuilder(args);varapp=builder.Build();app.MapGet("/product/{name}",(stringname)=>$"Theproductis{name}").WithName("product&......
  • Secure Code Warrior Introduction to OWASP Top 10 Awareness (with latest updates
    MissingFunctionAccessControlAccesstothesefunctionalitiesshouldberestrictedtoauthenticatedusers.However,thecurrentmechanismonlycheckswhetherauserexists.Anyuser,authenticatedornot,willbeabletoaccessrestrictedinformation.U......
  • ASP.NET Core使用Hangfire定时发布文章
    ASP.NETCore使用Hangfire实现定时任务前言也是上了5天班,终于迎来了休息,抽空更新下博客,然后就是下周一公司会对我进行考核,希望考核能通过吧!!!然后我想给博客添加一个定时发布文章的功能,其实这个功能对于我的博客是没什么作用的,什么时候发都没什么人看。但是咱还是要有这个功能。......
  • asp.net 分片下载
    参考:https://learn.microsoft.com/en-us/answers/questions/726990/serving-video-file-stream-from-asp-net-core-6-minihttps://khalidabuhakmeh.com/partial-range-http-requests-with-aspnet-core HTTPRangeRequestsandPartialResponsesWithASP.NETCoreLongg......
  • sprintf、snprintf、vsprintf、asprintf、vasprintf函数
    1.sprintfexternintsprintf(char*__restrict__s,constchar*__restrict__format,...);2.snprintf/*MaximumcharsofoutputtowriteinMAXLEN.*/externintsnprintf(char*__restrict__s,size_t__maxlen,......
  • 无涯教程-ASP.NET MVC - 单元测试
    在计算机编程中,单元测试是一种软件测试方法,通过该方法可以测试源代码的各个单元以确定它们是否适合使用。换句话说,这是一个软件开发过程,其中应用程序的最小可测试部分(称为单元)被单独且独立地检查以确保其正常运行。单元测试通常是自动化的,但也可以手动完成。单元测试的目标......
  • 无涯教程-ASP.NET MVC - 模型绑定
    ASP.NETMVC模型绑定允许您将HTTP请求数据与模型进行映射,使用浏览器在HTTP请求中发送的数据创建.NET对象的过程。模型绑定是HTTP请求和C#操作方法之间精心设计的桥梁,由于POST和GET会自动传输到您指定的数据模型中,因此开发人员可以轻松使用表单上的数据,ASP.NETMVC使用默认联编......
  • 无涯教程-ASP.NET MVC - 选择器
    ActionSelector是可以应用于Action方法的属性,用于响应请求而调用哪种Action方法,它有助于路由引擎选择正确的操作方法来处理特定请求。在编写Action方法时,它起着至关重要的作用。这些选择器将根据操作方法来决定方法调用的行为,它通常用于为操作方法的名称加上别名。ActionSele......
  • 无涯教程-ASP.NET MVC - 控制器
    控制器本质上是ASP.NETMVC应用程序的中央单元,控制器决定将选择哪个模型,然后在呈现该视图之后,从模型中获取数据并将其传递给相应的视图。控制器是从System.Web.Mvc.Controller继承的C#类,System.Web.Mvc.Controller是内置的控制器基类,控制器中的每个公共方法都称为操作方法,这意味......