优雅地在.NET环境中最小化动态生成的JavaScript? [英] Elegantly minify dynamically generated javascript in a .NET environment?

查看:91
本文介绍了优雅地在.NET环境中最小化动态生成的JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以编程方式从.NET Web应用程序创建javascript文件,并希望在将其传递给客户端之前将其最小化.有一种有效的方法可以做到这一点吗?

I'm programmatically creating javascript files from a .NET web app and would like to minify it before passing it to the client. Is there an efficient technique for doing this?

推荐答案

如果您只是想在将C#中的JavaScript字符串保存到文件之前使其最小化,则可以使用MS Ajax Minifier .net的YUI压缩器.这两个都公开了一个API,您可以执行此操作.这是使用ajax缩小器的示例:

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);

使用用于.net的YUI压缩器:

Using the YUI Compressor for .net:

var minifiedString = JavaScriptCompressor.Compress(unMinifiedString);

ajax minifier和YUI Compressor库都可以通过Nuget获得.

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

这篇关于优雅地在.NET环境中最小化动态生成的JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆