I'd like to have some of the ScriptManager features in the new Asp.net MVC model:
1- Script combining
2- Resolving different paths for external Javascript files
3- Minify and Gzip Compression
Here is what I found, but I'm not sure is the best way for MVC approach. In general what is a good approach to deal with Javascript code in the MVC model?
MVC 4 now includes Bundling and Minification helpers. You define all of the scripts that go into your bundle, and MVC will take care of bundling, compression, cache busting, etc.
http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification
Maybe you could just create a new 'Scripts' controller with different actions serving different combinations of compressed JS files. Since MVC is designed with a resource oriented approach, i.e. URLs are now at the center of your programming model, why not define simple URIs for your Javascripts too ?
In your views, for example you could reference your files like this :
This would call your 'all' action, resulting in all your compressed script files being sent.
Of course you would need to code the combining and compressing for now, or maybe reuse the Scriptmanager internally, I don't know if it's possible.
This is just an idea though, currently I'm referencing individual javascript files directly in my master pages.
Or how about including the ScriptManager itself, as the sole inhabitant of a solitary, once-per-page
<form runat="server">
?Like this:-
Works for me.
P.S. You'll need to ensure that this form tag never gets embedded within another form. Nested forms don't work.
Found this researching much the same problem: A Simple ScriptManager for ASP.NET MVC - written after this question was answered so added for reference.
In the first instance I'm going with the brute force solution i.e. stick it all in the master page (especially as one can now pull jQuery from Microsoft's CDN) - then we're going to investigate options for more optimal solutions.
Try this: http://www.codeplex.com/MvcScriptManager