title: C# Razor Page - Include @section Scripts inside _Layout (Dot net core)
body: <p>Assume the following <code>@section</code> code for a dynamic script received from <code>Controller/Model</code>: <em>(dotnet core v3)</em></p>
  <pre><code>        string scriptHtml = ViewData[&quot;scriptData&quot;];
          @section Scripts {
              @if(! string.IsNullOrEmpty(scriptHtml)){
                  &lt;script type='text/javascript'&gt;
                      @Html.Raw(scriptHtml)
                  &lt;/script&gt;
                  &lt;script type='text/javascript' src='~/js/appcore/site.reuse.js'&gt;&lt;/script&gt;
              }
          }
  </code></pre>
  <p>I want to use this <strong>inside _Layout.cshtml</strong>, in other words: I want to be able to include a Scripts section inside <code>_Layout.cshtml</code>, that will be consistent across all pages that include it.</p>
  <p>So basically, <code>_Layout.cshtml</code> should look like this (Only the Javascript section showed):</p>
  <pre><code>
  @section Scripts {
      @if(! string.IsNullOrEmpty(scriptHtml)){
           &lt;script type='text/javascript'&gt;
                @Html.Raw(scriptHtml)
            &lt;/script&gt;
            &lt;script type='text/javascript' src='~/js/appcore/site.reuse.js'&gt;&lt;/script&gt;
       }
  }
      &lt;footer class=&quot;border-top footer text-muted&quot;&gt;
      &lt;script src=&quot;~/lib/jquery/dist/jquery.min.js&quot;&gt;&lt;/script&gt;
      &lt;script src=&quot;~/lib/bootstrap/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;
      &lt;script src=&quot;~/js/site.js&quot; asp-append-version=&quot;true&quot;&gt;&lt;/script&gt;
      @RenderSection(&quot;Scripts&quot;, required: false)  &lt;!-- not rendered because it is not in the parent 'ie. Index.cshtml' page....
  </code></pre>
  <p>When this <code>@section</code> is <em>not inside _Layout.cshtml</em> but moved to the parent razor page (for instance Index.cshtml) calling <code>_Layout.cshtml</code> it will be included correctly.  This however means I will have to <code>@section</code> this script for every razor page.</p>
  <p>What can I do to include a 'dynamic' Javascript script and make it reusable across all pages that use <code>_Layout.cshtml</code>?</p>