Pergunta

Eu gostaria de ter alguns dos recursos do ScriptManager no novo modelo ASP.NET MVC:

1- Script combinando
2- Resolvendo caminhos diferentes para arquivos JavaScript externos
3- Minify e GZIP Compression

Aqui É o que eu encontrei, mas não tenho certeza se é a melhor maneira de abordagem do MVC. Em geral, qual é uma boa abordagem para lidar com o código JavaScript no modelo MVC?

Foi útil?

Solução

Talvez você possa simplesmente criar um novo controlador 'scripts' com ações diferentes que servem diferentes combinações de arquivos JS comprimidos. Como o MVC foi projetado com uma abordagem orientada a recursos, os URLs do IE estão agora no centro do seu modelo de programação, por que não definir URIs simples para seus javascripts também?

Em suas opiniões, por exemplo, você pode fazer referência a seus arquivos como este:

<script src="http://your_domain/scripts/all"/>

Isso chamaria sua ação 'All', resultando em todos os seus arquivos de script compactados sendo enviados.

É claro que você precisaria codificar a combinação e a compactação por enquanto, ou talvez reutilizar o scriptManager internamente, não sei se é possível.

Porém, é apenas uma idéia, atualmente estou referenciando arquivos JavaScript individuais diretamente em minhas páginas mestras.

Outras dicas

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

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.

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.

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top