合并、最小化和 gzip ASP.NET MVC 的 CSS 和 JavaScript 文件
再会!
我正在寻找组合、最小化和 gzip CSS 和 JavaScript 文件的解决方案。它们似乎有两种形式:
- 以 ASP.NET 处理程序\模块的形式,动态处理文件(带有缓存结果)
- 以 VS 构建任务的形式(在构建时执行处理)
一般来说,我都可以接受。
我查看了许多解决方案(并且我使用本文中的 ASP.NET 处理程序 http ://www.codeproject.com/KB/aspnet/httpcompression.aspx 很多),但也许有些“必须有”的东西出现了,但我错过了。
提前致谢!
Good day!
I'm looking for solution to combine, minimize and gzip CSS and JavaScript files. It seems they come in two forms:
- In form of ASP.NET handler\module with processing files on the fly (with caching results)
- In form of VS build tasks (to perform processing while building)
Generally I'm ok with either.
I've looked on a number of solutions (and I use ASP.NET handler from this article http://www.codeproject.com/KB/aspnet/httpcompression.aspx a lot), but maybe something "must have" came out and I've missed it.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我给你的建议是:使用构建任务并使用 HTTP 缓存输出。
在构建任务方面,您需要查看您最喜欢的 JavaScript 压缩器(我最喜欢的是 Google Closure Minifier)它有一个命令行实用程序,您可以将其插入项目文件、MSBUILD 文件或 NANT 文件中。 CSS 也是如此(我个人使用 Yahoo! YUI Compressor)。如果您喜欢使用 LESS,您当然可以将其与 YUI 压缩器结合使用。为了优化图像,我会使用 optipng。有关于这些人如何在其个人网站上工作的说明。
现在,在您完成这些文件并进行优化后,您将需要使用 MVC 的处理程序或控制器操作来输出它们。要设置过期时间,以便后续请求默认为第一个请求时下载的文件,您需要在代码中运行:
您很可能需要缓存破坏策略,以便可以更改内容文件。您可以通过将随机参数传递给处理程序来完成此操作。有几种不同的方法可以解决这个问题……只需谷歌一下即可。
希望这有帮助。
Here's my advice to you: use build tasks and HTTP cache the output.
In terms of build tasks, you'll want to check out your favorite JavaScript minifier (my favorite is Google Closure Minifier) that has a command line utility that you can just plug into your project file, MSBUILD file or NANT file. Same deal with CSS (I personally use Yahoo! YUI Compressor). If you're into using LESS, you can certainly combine this with the YUI compressor. To optimize images, I'd use optipng. There's directions on how these guys work on their individual sites.
Now, after you have these files all nice and optimized, you'll want to output them using a handler or controller action for MVC. To set the expiration so that subsequent requests will default to the file downloaded on the first request, you'll want this to run in your code:
More than likely you'll want a cache-buster strategy so that you can change the content files. You'd do this by passing a random parameter to your handler. There are a few different ways to go about this... just Google it.
Hope this helps.
我正在为中小型网站使用 telerik mvc 组件。使用 NuGet 添加和配置非常简单。
I'm using the telerik mvc components for small-medium sites. It was simple to add and configure with NuGet.
Moth 可以(除其他外)即时处理所有 javascript / css 请求。请参阅Wiki:Javascript。
最重要的是,它还可以将所有 javascript 放在页面底部,包括您在部分视图中编写的部分! Wiki:内联脚本。
Moth can (among other things) handle all your javascript / css requests on the fly. See Wiki: Javascript.
Best of all, it can also put all javascript at the bottom of the page, including parts you write in your partial views! Wiki: Inline script.