Answer the question
In order to leave comments, you need to log in
How to transfer all JavaScript/CSS to the Header of the page in ASP.NET MVC?
In order to avoid duplication of scripts and styles, an Extension method was written that registers a script/style and prevents it from being added again in the future, it looks something like this:
<%=AddScript("~/scripts/myscript.js")% >
Now we need to make sure that all these script/style declarations are inserted into the header. The problem is that if you render the header, then the collection with registered scripts is not yet filled. since the header is at the beginning of the document, and the addition of scripts / styles can be anywhere and in different Views.
I would appreciate any advice that will point me in the right direction.
Answer the question
In order to leave comments, you need to log in
Make a master page that is responsible for displaying the header (and something else that can be stuffed into it) - it should be a master for everyone and itself should not have AddScript calls after inserting all scripts - then it turns out that all AddScript are called before inserting them into the header.
And how to achieve this if you write on Razor? After all, there is no master page
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question