17

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?

1

5 Answers 5

18

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"/>

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

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

MvcScriptManager is aimed to port certain key features available in AjaxControlToolkit's ToolkitScriptManager into the current ASP.NET MVC Framework. You will be able to use it as a control in your ASP.NET MVC application.

Features

  1. Script combination (or concatenation). Scripts declared with MvcScriptManager will be combined on the fly into a single script file request when the page is rendered.
  2. Script minification (or crunching) in release mode. Minification process is done only once at the first request that references the specific script. Subsequent requests will use the crunched script content in cache (see #5 for detail). Crunching can be enabled/disabled for each script.
  3. Render localized resources for stand-alone script files. Localized strings will be appended to the script if specified.
  4. Support configurable HTTP compression and expiration setting when outputing scripts.
  5. Script caching with file dependency. Script file content is cached so that rendering combined script file will be much more performant. Cache dependency is linked to the physical file therefore any script update in the file system will be reflected in the cache instantly.
  6. Support rendering scripts in debug/release mode based on the running environment.
  7. Resolving different paths for stand-alone script files.
  8. Support multiple MvcScriptManagers on a single page (or master page). Support both Master and Slave rendering mode so that scripts declared with one ScriptManager can be rolled over to another one for rendering.
  9. Support web farm scenario...

Comments

3

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.

Comments

0

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.

2 Comments

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 - 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...
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.