结合,最小化和gzip为ASP.NET MVC的CSS和JavaScript文件

时间:2021-09-18 07:16:07

Good day!

I'm looking for solution to combine, minimize and gzip CSS and JavaScript files. It seems they come in two forms:

我正在寻找组合,最小化和gzip CSS和JavaScript文件的解决方案。它们似乎有两种形式:

  • In form of ASP.NET handler\module with processing files on the fly (with caching results)
  • 以处理器文件的ASP.NET处理程序\模块的形式(具有缓存结果)

  • In form of VS build tasks (to perform processing while building)
  • 以VS构建任务的形式(在构建时执行处理)

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.

我已经看了很多解决方案(我在本文中使用ASP.NET处理程序http://www.codeproject.com/KB/aspnet/httpcompression.aspx很多),但也许“必须有”的东西出来了我错过了

Thanks in advance!

提前致谢!

3 个解决方案

#1


6  

Here's my advice to you: use build tasks and HTTP cache the output.

这是我给你的建议:使用构建任务和HTTP缓存输出。

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.

在构建任务方面,您需要查看您最喜欢的JavaScript缩小器(我最喜欢的是Google Closure Minifier),它具有一个命令行实用程序,您只需插入项目文件,MSBUILD文件或NANT文件即可。同样处理CSS(我个人使用Yahoo! YUI Compressor)。如果你正在使用LESS,你当然可以将它与YUI压缩器结合使用。为了优化图像,我使用了optipng。关于这些人如何在他们的个人网站上工作的方向。

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:

现在,在您拥有这些文件并且优化之后,您将需要使用MVC的处理程序或控制器操作来输出它们。要设置到期时间,以便后续请求默认为第一个请求下载的文件,您需要在代码中运行:

Response.ExpiresAbsolute = DateTime.Now.AddYears(1);

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.

希望这可以帮助。

#2


2  

I'm using the telerik mvc components for small-medium sites. It was simple to add and configure with NuGet.

我正在将telerik mvc组件用于中小型站点。使用NuGet添加和配置非常简单。

#3


2  

Moth can (among other things) handle all your javascript / css requests on the fly. See Wiki: Javascript.

Moth可以(除其他外)在运行中处理所有的javascript / css请求。请参阅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.

最重要的是,它还可以将所有javascript放在页面底部,包括您在部分视图中编写的部分! Wiki:内联脚本。

#1


6  

Here's my advice to you: use build tasks and HTTP cache the output.

这是我给你的建议:使用构建任务和HTTP缓存输出。

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.

在构建任务方面,您需要查看您最喜欢的JavaScript缩小器(我最喜欢的是Google Closure Minifier),它具有一个命令行实用程序,您只需插入项目文件,MSBUILD文件或NANT文件即可。同样处理CSS(我个人使用Yahoo! YUI Compressor)。如果你正在使用LESS,你当然可以将它与YUI压缩器结合使用。为了优化图像,我使用了optipng。关于这些人如何在他们的个人网站上工作的方向。

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:

现在,在您拥有这些文件并且优化之后,您将需要使用MVC的处理程序或控制器操作来输出它们。要设置到期时间,以便后续请求默认为第一个请求下载的文件,您需要在代码中运行:

Response.ExpiresAbsolute = DateTime.Now.AddYears(1);

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.

希望这可以帮助。

#2


2  

I'm using the telerik mvc components for small-medium sites. It was simple to add and configure with NuGet.

我正在将telerik mvc组件用于中小型站点。使用NuGet添加和配置非常简单。

#3


2  

Moth can (among other things) handle all your javascript / css requests on the fly. See Wiki: Javascript.

Moth可以(除其他外)在运行中处理所有的javascript / css请求。请参阅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.

最重要的是,它还可以将所有javascript放在页面底部,包括您在部分视图中编写的部分! Wiki:内联脚本。