I’m fairly new to ASP.NET MVC, and I’m having a little trouble with scripts… in particular, I want to use jQuery in most pages, so it makes sense to put it in the master page. However, if I do (from my ~/Views/Shared/Site.Master):
<script src='../../Scripts/jquery-1.2.6.js' type='text/javascript'></script>
Then that is literally what goes down to the client – which of course only works if our current route happens to have the right number of levels. Starting with ~/Scripts/... doesn’t work. Starting with /Scripts/... would only work if the project was at the site root (which I don’t want to assume).
I have one working approach (I’ll post below) – but: am I missing something?
I’d rather not have to involve a script-manager, as that seems to defeat the simplicity of the ASP.NET MVC model… or am I worrying too much?
Here’s the way I can get it working, which works also for non-trivial virtuals – but it seems over-complicated:
<script src='<%=Url.Content('~/Scripts/jquery-1.2.6.js')%>' type='text/javascript'></script>
I have a AppHelper class with some methods for adding script references:
so in your master page you can use: