locked
RenderSection Scripts aspnet core RRS feed

  • Question

  • User350142465 posted

    Hello I`m new in asp net core, I`m comming from aspnet framework 

    Today I create a new project aspnet core for study and I saw that Visual Studio create a structure with a folder named "pages>Shared>_Layout.cshtml", as aspnet framework I understand this layout is a comum html to my pages. 

    when I opened this file I can see a line with code "@RenderSection("Scripts", required: false)"

    in aspnet Framework this "Scripts" is a bundle script file that is create on bundle.cs but in aspnet core I couldn`t find this "Scripts" where it is ? how can I add new *.js files in it? 

    Tuesday, April 7, 2020 3:46 PM

Answers

  • User475983607 posted

    where should I create it ? in what file? (I mean in best practices)

    and If it is not created yet by default, why visual studio try "add a file that not exists" ? 

    The scripts section goes in the View.  It's just a tool you can use to insert scripts at the bottom of the _layout.ashtml page.

    @{
        ViewData["Title"] = "Home Page";
    }
    
    <h1>Index</h1>
    
    @section scripts{ 
        <script src="~/lib/jquery/dist/jquery.min.js"></script>
        <script>
            //JavaScript 
        </script>
    }

    You might be interest in reading the reference documentation.

    https://docs.microsoft.com/en-us/aspnet/core/mvc/views/layout?view=aspnetcore-3.1

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 7, 2020 4:12 PM

All replies

  • User475983607 posted

    Use the standard <script> syntax.

    @section scripts{ 
        <script src="~/lib/jquery/dist/jquery.min.js"></script>
        <script>
            //JavaScript 
        </script>
    }

    Tuesday, April 7, 2020 3:59 PM
  • User350142465 posted

    where should I create it ? in what file? (I mean in best practices)

    and If it is not created yet by default, why visual studio try "add a file that not exists" ? 

    Tuesday, April 7, 2020 4:06 PM
  • User475983607 posted

    where should I create it ? in what file? (I mean in best practices)

    and If it is not created yet by default, why visual studio try "add a file that not exists" ? 

    The scripts section goes in the View.  It's just a tool you can use to insert scripts at the bottom of the _layout.ashtml page.

    @{
        ViewData["Title"] = "Home Page";
    }
    
    <h1>Index</h1>
    
    @section scripts{ 
        <script src="~/lib/jquery/dist/jquery.min.js"></script>
        <script>
            //JavaScript 
        </script>
    }

    You might be interest in reading the reference documentation.

    https://docs.microsoft.com/en-us/aspnet/core/mvc/views/layout?view=aspnetcore-3.1

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 7, 2020 4:12 PM