从MVC中生成的HTML中删除多余的空格 [英] Removing extra whitespace from generated HTML in MVC
问题描述
我有一个MVC应用程序视图生成相当大的HTML表值(> 20MB)。
I have an MVC application view that is generating quite a large HTML table of values (>20MB).
我使用压缩压缩控制器中的视图过滤器
I am compressing the view in the controller using a compression filter
internal class CompressFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpRequestBase request = filterContext.HttpContext.Request;
string acceptEncoding = request.Headers["Accept-Encoding"];
if (string.IsNullOrEmpty(acceptEncoding))
return;
acceptEncoding = acceptEncoding.ToUpperInvariant();
HttpResponseBase response = filterContext.HttpContext.Response;
if (acceptEncoding.Contains("GZIP"))
{
response.AppendHeader("Content-encoding", "gzip");
response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
}
else if (acceptEncoding.Contains("DEFLATE"))
{
response.AppendHeader("Content-encoding", "deflate");
response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
}
}
}
消除在运行压缩过滤器之前在视图中生成的(相当大的)大量的冗余空白空间(以减少压缩工作量和大小)?
Is there a way to also eliminate the (quite large) amount of redundant whitespace generated in the view before I run the compress filter (to reduce compression workload and size)?
/ b>
我使用下面的Womp建议的WhiteSpaceFilter技术工作。
I got it working using the WhiteSpaceFilter technique suggested by Womp below.
感兴趣的是这里的结果,由Firebug分析:
For interest here's the results, as analysed by Firebug:
1)无压缩,无空白条 - 21MB,2.59分钟
3)使用GZIP压缩,空白条 - 558kB,12.77s
\\ b $ b因此值得。 / p>
1) No Compression, no whitespace strip - 21MB, 2.59 minutes
2) With GZIP compression, no whitespace strip - 2MB, 17.59s
3) With GZIP compression, whitespace strip - 558kB, 12.77s
So certainly worth it.
推荐答案
这个家伙写了一个整洁的小空格压缩器,它简单地通过正则表达式运行你的字节的快速块副本,以剥离空间的blob。他把它作为一个http模块,但你可以把7行的workhorse代码,并把它拖入你的函数。
This guy wrote a neat little whitespace compactor that simply runs a fast block copy of your bytes through a regular expression to strip out blobs of space. He wrote it as an http module, but you could take the 7 lines of workhorse code out of it and plop it into your function.
这篇关于从MVC中生成的HTML中删除多余的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!