Elegantly minify dynamically generated javascript in a .NET environment?

Why not to use javascript-written minifier directly in .NET (try if it works as JScript code). Uglify.js comes to mind...


If you simply want to be able to minify a javascript string in C# before saving it to a file, I would use either the MS Ajax Minifier or the YUI compressor for .net. Both of these expose an API that allows you to do this. Here is a sample using the ajax minifier:

var minifier = new Microsoft.Ajax.Utilities.Minifier();
var minifiedString = minifier.MinifyJavaScript(unMinifiedString);

Using the YUI Compressor for .net:

var minifiedString = JavaScriptCompressor.Compress(unMinifiedString);

Both the ajax minifier and and YUI Compressor libraries are available via Nuget.