缩小 ASP.NET 应用程序的 Html 输出
有哪些方法可以减少 ASP.NET 应用程序发送的 HTML 响应的大小?
我正在使用不属于我的控件,它会生成带有空格的输出。 我有兴趣缩小页面的整个 HTML 输出,就像 google 所做的那样(查看源代码 www.google.com)以改善计时。
是否有任何可用于 ASP.NET 的实用程序类可以为我做这些事情?
What are the ways by which we can reduce the size of the HTML Response sent by an asp.net application?
I am using Controls which are not owned by me and it produces output with white spaces. I am interested in Minifying the entire HTML output of the page just like how google does (View source www.google.com) to improve the timing.
Is there any Utility classes available for ASP.NET which can do this stuff for me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
无需在运行时执行此操作。 因为它可以在编译时完成。
详细信息: http://omari-o .blogspot.com/2009/09/aspnet-white-space-cleaning-with-no.html
There is no need to do it at run time. Because it can be done at compile time.
Details: http://omari-o.blogspot.com/2009/09/aspnet-white-space-cleaning-with-no.html
尝试按照此处所述的 HTTP 模块: http://madskristensen .net/post/a-whitespace-removal-http-module-for-aspnet-20
Try HTTP module as described here: http://madskristensen.net/post/a-whitespace-removal-http-module-for-aspnet-20
对于 Microsoft .NET 平台,有一个名为 WebMarkupMin 的库,它可以缩小 HTML 代码。 每个 ASP.NET 框架都有自己的模块:
文档位于 - http://webmarkupmin。 codeplex.com/documentation
For Microsoft .NET platform there is a library called the WebMarkupMin, which produces the minification of HTML code. For each ASP.NET framework has its own module:
Documentation is available at - http://webmarkupmin.codeplex.com/documentation
我想对 Thorn 的建议发表评论(但我是堆栈溢出的新手)。
链接代码 (omari-o.blogspot.com) 不支持 MVC4,尽管该代码是开源的,但由于 MVC3 和 MVC4 之间的制动变化而无法轻松升级。
在运行时,http 结果中可能会写入空格,只有实际站点的开发人员才能知道这一点。 因此,模板文件(aspx)的静态缩小根本不是万无一失的。 gius 建议的动态缩小应该用于保证正确删除空格,不幸的是这会产生运行时计算成本。 如果代码动态将空格写入输出,则必须动态将其删除。
I want to comment on Thorn's suggestion (but I'm new to stack overflow).
The linked code (omari-o.blogspot.com) doesn't support MVC4, and although the code is open source it cannot easily be upgraded because of braking changes between MVC3 and MVC4.
There might be whitespaces written to the http result at runtime, only the developer of the actual site can know that. Thus static minification of template files (aspx) is not foolproof at all. Dynamic minification, which is suggested by gius, should be used to guarantee that whitespaces are removed correctly, and unfortunately this will incur a runtime computation cost. If code dynamically writes spaces to the output, it will have to be removed dynamically.
接受的答案不适用于 MVC 4,因此这里有一个类似的库,可以在构建时缩小 https://github .com/jitbit/HtmlOptimizerMvc4
The accepted answer does not work with MVC 4, so here is a similar lib that minifies at build-time https://github.com/jitbit/HtmlOptimizerMvc4
只是添加另一个我没有在此处列出的选项,这是我建议使用的选项:
Html minifier 命令行工具用法
:
此处 和 此处
然而,使用此工具有一个问题:它留下单行 (// ) 注释,并且它会导致 Razor 解析出现问题,因为放置在 C# 块中的单行注释如下所示:
将导致该行的缩小输出其余部分从此时起被 Razor 解析器忽略,这将导致因此会引发一个错误,指出找不到该块的结束“}”。
我解决此问题的方法是从输出中完全删除这些注释。
这样就可以了。
为此,只需从第 145 行删除 RegexOptions.SingleLine:
Just adding another option I do not see listed here, which is the one I was recommended using:
Html minifier command line tool
Usage:
here and here
There is an issue, however, with this tool: it leaves single line (//) comments, and it causes problems for Razor parsing, since a single line comment placed within a C# block like the following:
will cause the minification output rest of the line, from this point on, to be ignored by the Razor parser, which will thus raise an error stating there it could not find the closing "}" for the block.
My workaround for this issue was to completely removing these comments from the output.
This way it works.
To do that, simply remove the RegexOptions.SingleLine from line 145:
在母版页或 Web 表单页面后面、Page_Load 事件之前使用此函数:
Use this function in behind of master page or web form page, before Page_Load event: