Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
57
rated 0 times [  64] [ 7]  / answers: 1 / hits: 22763  / 15 Years ago, tue, june 2, 2009, 12:00:00

What is the correct way to reference Javascript in ASP.NET MVC? Using something like ../../Scripts/Myscript.js seems to work fine for routes that are the traditional {controller}/{action}/{id}, but are more fragile for anything more or less complex than that. Of greater concern is that the rational absolute reference (/Scripts/Myscript.js) breaks Intellisense in Visual Studio.



How do you handle it?



EDIT: This is obviously a very old question at this point, but I'm editing to mention that in MVC4, all you need is this:



src=~/Scripts/Whatever.js


That's enough for Razor to figure out where you mean, using the root path.


More From » asp.net

 Answers
8

<script src=<%= Url.Content(~/Scripts/Myscript.js) %> type=text/javascript></script>


[#99410] Thursday, May 28, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jarod

Total Points: 62
Total Questions: 111
Total Answers: 83

Location: Saint Vincent and the Grenadines
Member since Sat, Sep 11, 2021
3 Years ago
jarod questions
;