B
B
Bartez2011-05-29 06:54:03
ASP.NET
Bartez, 2011-05-29 06:54:03

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

2 answer(s)
Z
zandroid, 2011-05-31
@zandroid

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.

N
Nikolai Kupstas, 2011-08-12
@Illorian

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 question

Ask a Question

731 491 924 answers to any question