views:

3275

answers:

4

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 javascipt files
3- Minify and Gzip Compression

Here is what I found but not sure is the best way for mvc approach.
In general what is the your approach to deal with javascript code in the MVC model?

+14  A: 

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 :

<script src="http://your_domain/scripts/all"/&gt;

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.

Franck
I guess it has to be a static js file with different domain name in URL (cookieless).In that case ScriptController not works.
+10  A: 

try this: http://www.codeplex.com/MvcScriptManager

+3  A: 

Or how about including the ScriptManager itself, as the sole inhabitant of a solitary, once-per-page <form runat="server"> ?

Like this:-

   <form runat="server">
      <asp:ScriptManager ID="ScriptManager1" runat="server" EnableScriptGlobalization="true">
      </asp:ScriptManager>
    </form>

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.

Andrew Webb
A: 

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.

Murph
Although that looks nice, it doesn't seem to combine or compress anything. It only allows you to easily include scripts only once on the master page.
luckyllama
@luckyllama - fair point, the problem is that this is a complex problem. I'd suggest that minifying should, ideally, happen at build time not run time. Combining is a grey area - especially if you have different combinations of script on each page - because you're losing the benefit of caching. Compression should probably be handled by the server and multiple sources are a whole other question unless its being suggested that the same script might come from two places which I'd have problem with anyway...
Murph