Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to place @Scripts.Render and @Styles.Render

I am quite confused about where to place @Scripts.Render and @Styles.Render. Ideally I would put them all inside head section but unexpectedly, for example, @Scripts.Render("~/bundles/bootstrap") won't work at all unless I put it inside body section, after @RenderBody().

Same confusion for where to place @Mvc.RazorTools.BundleManager.Styles.Render() and @RenderSection("scripts", required: false).

A final word about them would be really appreciated.

p.s. There is another similar answer to this question but the only similarity is in the title as the problem was in missing files, not in placement. Also that question does not address both scripts AND styles.

Adding bundles for sake of completion:

bundles.Add(new ScriptBundle("~/bundles/bundlejquery").Include(
                    "~/Scripts/jquery-1.12.4.js",
                   "~/Scripts/jquery-ui-1.12.1.js",
                   "~/Scripts/jquery.validate.min.js",
                   "~/Scripts/jquery.validate.unobtrusive.min.js"));

bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                  "~/Scripts/bootstrap.js",
                  "~/Scripts/respond.js"));
like image 518
Luke Avatar asked Dec 07 '25 08:12

Luke


1 Answers

First of all, you can put @Styles.Render and @Scripts.Render wherever you put script and style tags, so it is totally fine to put in header. Probably there is something else wrong that if you provide more information, it might help. you usually put @RenderSection("scripts", required: false) inside your master page(like the default _layout file). Then you can put

@section script{
}

on your page then the content that you put inside this tag would be replaced with @RenderSection("scripts", required: false) on master page.

Note: you cannot put the @section on the partial page.

like image 187
Nick Mehrdad Babaki Avatar answered Dec 10 '25 00:12

Nick Mehrdad Babaki