javascript - What is the best way to include script references in ASP.NET MVC views? -


as know, asp.net mvc stores view markup in views directory, hierarchically incompatible url routes used in asp.net mvc web application. on opposite end, in asp.net web forms (and in asp.net mvc, too), urls can , have nested "directories", or rather path separators, , combined fact web applications not hosted in root path of url rather in sub-directory i.e. "/stuff/here/myactualapp", necessary use script path relative root of application rather relative root of url. meanwhile, however, visual studio script intellisense dictates urls map relatively file being edited.

further, i've run lot of problems using runat="server" virtualize root path support "~/", such head tag needing runat="server", , introduces kinds of other constraints.

finally, 1 more thing: if minified flavor of script jquery referenced in addition intellisense-ready flavor, visual studio balk on it. have use escaped code keep vs balking.

so i've been using syntax, or variations of it, in visual studio 2010 since vs 2005 including script in asp.net view markup deal discrepancies nested folders asp.net mvc view files (which not line actual urls) need use vsdoc flavor of jquery instead of minified version intellisense working.

<%if (false) { %> <script src="../../scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script> <% } %> <%= "<script type=\"text/javascript\"" src=\""    + resolveurl("~/scripts/jquery-1.4.1.min.js") + "\"></script>"%> 

aside using cdn url, there better way this? it's ugly. wish microsoft have addressed without resorting scriptmanager tags (which require server-side forms make markup more verbose).

note: issue not intellisense support last line in code above, having emit line rather using real markup. however, want intellisense support readiness, too.

we use squishit. combines , minifies files, too, , supports css (and dotless).

<head>   <%= bundle.css()     .add("~/media/css/style.less")     .add("~/media/css/print.css")     .add("~/media/css/media.css")     .render("~/media/css/styles_#.css") %>   <%= bundle.javascript()     .add("~/media/js/jquery-1.4.3.js")     .add("~/media/js/jquery.equalheights.js")     .add("~/media/js/jquery.cycle.lite.1.0.js")     .add("~/media/js/swfobject-2.2.js")     .add("~/media/js/site.js")     .render("~/media/js/js_#.js") %> </head> 

t4mvc can provide way reference url's:

<script src="<%= links.scripts.map_js %>" type="text/javascript"></script> 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -