dev.e.loper
dev.e.loper

Reputation: 36034

ASP.NET - script and css compression

Is there any native compression (for javascript/css files) available in ASP.NET?

Upvotes: 6

Views: 5861

Answers (9)

mrogunlana
mrogunlana

Reputation: 847

I just learned something today: you can run JavaScript via windows console. I'm a fan of cssmin.js; so, this plus windows console = win! All you have to do is download cssmin.js, put it in a folder on your web project and add the following post-build event in Visual Studio:

type "$(ProjectDir)css\*.css" | cscript //NoLogo "$(SolutionDir)tools\cssmin.js" > "$(ProjectDir)css\core.min.css"

Doing this keeps you from having to edit your project as ajaxmin would have you to do.

Upvotes: 0

Kelqualyn
Kelqualyn

Reputation: 509

Here is my way: Use MVC. Process js|css content via MVC controller's actions. Combine multiple files into one. Minify and obfuscate script|css on fly before it stored in cache. Cache results. Use CacheDependency. Enable gzip for dynamic content. Enable gzip before cache feature.

Everything can be done just by adding custom attributes on action methods, using ASP.NET MVC Js/Css Composer/Compressor.

Sample:

public class JsController : Controller
{
    [Utility.Processors.JsCompress]
    [OutputCache(Duration = 3600)]
    public ActionResult Jquery()
    {
        return View();
    }
}

You can derive from CustomTextPostProcessingAttribute and make your own postprocessing for any type of text content, you need.

Upvotes: 0

Andrew
Andrew

Reputation: 21

Try StyleManager for CSS combination and minification. It uses YUI Compressor under-the-hood.

Its usage is a lot like asp.net's ScriptManager, so it's quick to get used to. It's easy to add to your project too, only takes a minute.

Most importantly - it combines your CSS files too. So instead of having like 10 CSS files to download it'll just be 1, which will also be compressed etc.

Upvotes: 1

Evan Nagle
Evan Nagle

Reputation: 5133

Try Chirpy. It mashes, minifies, and validates your javascript, stylesheet, and dotless files. You can use YUI Compressor or Google Closure Compiler.

http://chirpy.codeplex.com/

Or, for more info, check out:

http://www.weirdlover.com/2010/05/22/visual-studio-add-in-for-dotless-js-and-css-files/

Upvotes: 6

gatapia
gatapia

Reputation: 3664

I have written something to do this for me, you can download it here: http://www.picnet.com.au/blogs/Guido/post/2009/12/10/Javascript-runtime-compilation-using-AspNet-and-Googles-Closure-Compiler.aspx

It uses Google's closure compiler which is pretty awesome.

Thanks

Guido

Upvotes: 0

Grant Wagner
Grant Wagner

Reputation: 25931

Further to other answers and comments, you can use Yahoo!'s YUI Compressor and make it an MSBuild Task to integration it into your build and deployment process.

Upvotes: 1

RedWolves
RedWolves

Reputation: 10385

In the appendix of Professional ASP.NET 3.5 Scott Hanselman talks about Packer for .NET. This will integrate with MSBuild and pack javascript files for production deployments etc.

Upvotes: 4

kay.herzam
kay.herzam

Reputation: 3063

You could use Packer.

This utlity supports JavaScript compression and /or "minifying", and CSS "minifying".

It's available as a command line utility or also as an MSBuild task. This way you can integrate it into your build process / Visual Studio project.

Upvotes: 4

Artem Koshelev
Artem Koshelev

Reputation: 10607

There is Gzip/Deflate compression support in IIS compatible with all modern browsers except IE6. For IIS 7 check this page: http://www.iis.net/ConfigReference/system.webServer/httpCompression

Upvotes: 1

Related Questions