This project is read-only.
The package has a couple different classes, handlers, and one main module in it. It's designed such that each part can be used separately with the exception of one class. Also, depending upon your setup, WebForms or MVC, you have a couple different options on set up.

Common Setup


No matter what you're going to need to make some modifications to your web.config. Specifically under modules (either system.webServer or system.web depending on if you're using IIS 7 or 6) you need to add the following:

<add name="Compression" type="OptimizerPrime.HttpModules.Compression.Compression, OptimizerPrime, Version=, Culture=neutral" />

Note that the DLL hasn't been signed, but you should be able to download the code and do that if need be. Anyway this module automatically minifies any page that is requested and sets the compression on the response to deflate or gzip if it is accepted by the client. This module will automatically generate a config file at App_Data/Configs/Compression.cfg. This file allows you to set which types of files, paths, etc. to ignore (no compression, no minification). The file will look like the following when generated:

<?xml version="1.0"?>
<CompressionConfig xmlns:xsi="" xmlns:xsd="">
<IgnorePages />

The IgnorePages part of the XML is a list of strings that contain regex expressions to identify files to ignore. For example:
<?xml version="1.0"?>
<CompressionConfig xmlns:xsi="" xmlns:xsd="">

Would tell it to ignore all aspx files. Setting it to this:
<?xml version="1.0"?>
<CompressionConfig xmlns:xsi="" xmlns:xsd="">

Will tell it to ignore Http handlers, JS files, and CSS files (note that if you are creating an MVC site, you must specify anything that is not a page as it will attempt to compress it. In the future I hope to be able to tie into the routing tables a bit more). With that you should be able to get the module working.


There are currently three handlers found in the package, one for JavaScript, one for CSS, and one for images. In order to set these up, you must add the following to your web.config:

<add verb="*" path="CSS.ashx" type="OptimizerPrime.HttpHandlers.CSSHandler, OptimizerPrime, Version=, Culture=neutral" />
<add verb="*" path="JS.ashx" type="OptimizerPrime.HttpHandlers.JavascriptHandler, OptimizerPrime, Version=, Culture=neutral" />
<add verb="*" path="Image.ashx" type="OptimizerPrime.HttpHandlers.ImageHandler, OptimizerPrime, Version=, Culture=neutral" />

In order to use each one you would do the following:


This would combine the two files, minify them, and compress them to be sent along the wire. Also it sets etags, etc. properly so that a client will only ask for the file once (assuming their cache isn't screwy).


Like the CSS handler, this would combine the two files, minify them, etc. Note that this shouldn't be done to already minified javascript files (I've run into issues with the minified jQuery files that I'm still working on). Also it sets etags, etc. properly.


This handler is a bit different. It requires a path to be set at run time in order to function properly. In the global.asax file, you must set the path that you intend to store images within at the application start. For instance:

protected void Application_Start(object sender, EventArgs e)
OptimizerPrime.HttpHandlers.ImageHandler.Folder = "~/Images/";

Would indicate that all images would be stored in the Images directory. This must be set for security reasons, otherwise this handler could be used to gain access to any/all files on the system (in the future I will have it check the file type, but for now set the folder path). It will only serve files found in the specified folder. But once set, any files in there it will serve to the user, setting etags, etc. appropriately.

WebForms Specific Code

There is one more class that is specific to WebForms. Specifically OptimizerPrime.BaseClasses.BasePage. This, as the name implies, is a base class for all Pages. It uses System.Web.UI.Page as a base class and overrides a couple of functions. This class is designed to help those that use themes. After the theme is applied, it will take all of the CSS files and automatically put them all into one call using the CSS handler (thus compressing that down for you automatically). Also it has the ability to compress the ViewState for you (note that you only want to do this in instances where you have a large view state as it can actually increase the size if it's only a couple of bytes). In doing so it will compress the ViewState object and save it in a seperate hidden field called ZIPSTATE. Note that both of these features are turned off by default and thus the properties CompressCSS and CompressViewState must be set to true in order for them to work. However once this is done, it should do everything automatically for you.

Last edited Mar 31, 2011 at 7:24 PM by JaCraig, version 1


No comments yet.