Skip to content

Using SquishIt programmatically without the file system

kamranayub edited this page Dec 20, 2011 · 15 revisions

If your environment does not allow reading/writing to the file system, you may be ready to turn your back on SquishIt. Don't! It is entirely possible to use SquishIt programmatically.

Setting up your Global.asax

The trick is to programmatically define your bundles in a Application_Bundle method of your Global.asax file. You will call this method in Application_Start.

protected void Application_Start() {
    // ... everything else

    // SquishIt
    Application_Bundle();
}

protected void Application_Bundle() {
    //SquishIt
    Bundle.Css()
	.Add("~/Content/Base.css")
	.Add("~/Content/MainStyle.css")
	.AsCached("main", "~/assets/css/main");
}

The key is the AsCached method. The first argument is the name of your bundle (or key) and the next is the server URL SquishIt should use (the {id} is the key name, e.g. main). As you can probably tell, we will need an AssetsController in our project.

Setting up an AssetsController

This is simple. If you are using the regular {id} in MVC for your identifier arguments, simply create a new controller and inherit from SquishItController.

However, if you are either using a custom BaseController or using a different name for the typical {id} field (e.g. {identifier}) you will need to copy the SquishItController code into your own controller. Be sure to specify that the content type is "text/css" for your CSS bundles. An example is below:

// could inherit directly from SquishItController but we use "identifier" as ID parameter in route not "id"
public class AssetsController : BaseController //: SquishItController
{
	public ActionResult Js(string identifier)
	{
	    return Content(Bundle.JavaScript().RenderCached(identifier), "text/javascript");
	}

	public ActionResult Css(string identifier)
	{
	    return Content(Bundle.Css().RenderCached(identifier), "text/css");
	}
}

Using SquishIt in your views

Almost done. Now you just need to render your scripts to your views. Luckily, it's one line of code!

@Bundle.CSS().MvcRenderCachedAssetTag("main")
@Bundle.JavaScript().MvcRenderCachedAssetTag("mainscripts")

You will need to import or set up the appropriate SquishIt namespace to use these in the views.

Clone this wiki locally