Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of "@section" in ASP.NET Core MVC?

In the default _Layout.cshtml file, scripts are defined in "environment"s like so:

<environment names="Development">
     <script src="~/lib/jquery/dist/jquery.js"></script>
     <script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
     <script src="~/js/site.js" asp-append-version="true"></script>
</environment>
<environment names="Staging,Production">
     <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-2.1.4.min.js"
                    asp-fallback-src="~/lib/jquery/dist/jquery.min.js"
                    asp-fallback-test="window.jQuery">
     </script>
     <script src="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.5/bootstrap.min.js"
                    asp-fallback-src="~/lib/bootstrap/dist/js/bootstrap.min.js"
                    asp-fallback-test="window.jQuery && window.jQuery.fn && window.jQuery.fn.modal">
     </script>
     <script src="~/js/site.min.js" asp-append-version="true"></script>
</environment>

And below that is @RenderSection("scripts", required: false)

I can't seem to implement a section (in this case "scripts") in any separate .cshtml file since it looks like they got rid of "@section" in Core

I would like to add specific scripts for specific views. What is the new way to go about this? Do I just dump everything in _Layout now?

like image 772
Daath Avatar asked Mar 25 '16 14:03

Daath


People also ask

What is @section scripts in MVC?

@section allows you to include those files only for certain views. It is needed since, a view cannot easily change the contents of the _Layout. cshtml otherwise. You can also position the @section at the bottom of the layout, for JavaScript files for example, or at the top of the layout, for CSS files.

What is strongly typed view in MVC?

Strongly typed views are used for rendering specific types of model objects, instead of using the general ViewData structure. By specifying the type of data, you get access to IntelliSense for the model class.

What is a razor view in MVC?

Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML.

What is Razor pages in ASP.NET Core?

Razor pages are simple and introduce a page-focused framework that is used to create cross-platform, data-driven, server-side web pages with clean separation of concerns.


1 Answers

I think you are mistaken. It works just fine in ASP.NET Core. I have this in my _layout.cshtml

@RenderSection("scripts", required: false)

and in one of my views I'm adding scripts there like this:

@section Scripts {
    @if (Model.CanEdit)
   {
      await Html.RenderPartialAsync("EditorScriptsPartial");
   }
}
like image 200
Joe Audette Avatar answered Oct 23 '22 14:10

Joe Audette