JavascriptHelper–Managing JS files for ASP.NET MVC - Online Free Computer Tutorials.

'Software Development, Games Development, Mobile Development, iOS Development, Android Development, Window Phone Development. Dot Net, Window Services,WCF Services, Web Services, MVC, MySQL, SQL Server and Oracle Tutorials, Articles and their Resources

Tuesday, June 12, 2012

JavascriptHelper–Managing JS files for ASP.NET MVC

After working several years with the Castle Monorail MVC framework, I decided to try ASP.NET MVC to see if it had caught up to Monorail. The transition seemed to go rather smoothly, but one area where I was surprised to find how clumsily it was handled, was the management of JavaScript files. Basically, if some part of a page, say a helper or partial page, needed a particular JS file, you had one of two choices.

The first option is to have the part itself would write the script tag. This allows the part to operate as a "black box" – just drop it in and it works – But it means that there will be script tags loading file scattered throughout the page, and that the part needs to know your folder structure where you keep your JavaScript files. And it needs to know if you want the file loaded from you website or from a CDS like Googleapi.com. And, since there's a good chance it will depend on jQuery, you have to make sure that jquery.js is loaded first, at the top of the page, despite "best Practices" which say script files should be loaded at the bottom of the page. Then, let's say, two different partial views on the same page use the same JS file, you need a way of making sure it's only included once. Plus, there's a good chance it will also depend on its own CSS file being loaded, which doubles the problems above. Microsoft (well, PluralSite's training videos on Microsoft's site) recommends putting the script tags in a @section named Scripts, and rendering that in the layout, which helps but only addresses some of the problems).


I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too. 

Stay tuned to my blogtwitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.


[ASP] [.NET] [Javascript] [Dev] [Intermediate]


No comments:

Post a Comment