缩小ASP的Html输出。网络应用程序

时间:2022-08-26 20:23:36

What are the ways by which we can reduce the size of the HTML Response sent by an asp.net application?

如何减少asp.net应用程序发送的HTML响应的大小?

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.

我正在使用不属于我的控件,它生成带有空格的输出。我感兴趣的是缩小页面的整个HTML输出,就像谷歌如何(查看源代码www.google.com)来改进时间。

Is there any Utility classes available for ASP.NET which can do this stuff for me?

是否有用于ASP的实用程序类。谁能帮我做这些事?

6 个解决方案

#1


31  

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://omari-o.blogspot.com/2009/09/aspnet-white-space-cleaning-with-no.html。

#2


19  

Try HTTP module as described here: http://madskristensen.net/post/a-whitespace-removal-http-module-for-aspnet-20

尝试如下所示的HTTP模块:http://madskristensen.net/post/a- whitespacase -removal-http- moduleforaspnet -20

#3


9  

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:

对于Microsoft . net平台,有一个名为WebMarkupMin的库,它生成HTML代码的简化。对于每一个ASP。NET框架有自己的模块:

  • ASP.NET Core - WebMarkupMin.Web. Implemented as an HTTP module, so it can work with any framework. Suitable for use in the ASP.NET Web Pages framework (Razor).
  • ASP。网络核心——WebMarkupMin.Web。实现为HTTP模块,因此可以使用任何框架。适合在ASP中使用。NET网页框架(Razor)。
  • ASP.NET MVC - WebMarkupMin.Mvc
  • ASP。净MVC - WebMarkupMin.Mvc
  • ASP.NET Web Forms - WebMarkupMin.WebForms
  • ASP。网络表单- WebMarkupMin.WebForms。

Documentation is available at - http://webmarkupmin.codeplex.com/documentation

文档可以在http://webmarkupmin.codeplex.com/documentation中找到

#4


6  

I want to comment on Thorn's suggestion (but I'm new to stack overflow).

我想评论一下Thorn的建议(但我对stack overflow很陌生)。

  1. 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.

    链接代码(omari-o.blogspot.com)不支持MVC4,虽然代码是开源的,但由于MVC3和MVC4之间的刹车变化,它不能轻松升级。

  2. 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.

    在运行时,可能会有白空间写入http结果,只有实际站点的开发人员才能知道这一点。因此,模板文件(aspx)的静态缩小并不是万无一失的。gius建议使用动态缩小来保证白空间被正确删除,不幸的是,这将导致运行时计算成本。如果代码动态地向输出写入空格,则必须动态地删除它。

#5


3  

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

公认的答案不适用于MVC 4,所以这里有一个类似的库,可以在构建时使用https://github.com/jitbit/htmloptimimzervc4进行压缩

#6


2  

Just adding another option I do not see listed here, which is the one I was recommended using:

只是添加了另一个我在这里没有看到的选项,这是我建议使用的:

Html minifier command line tool

Html迷你器命令行工具。

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:

但是,这个工具有一个问题:它只留下一行(//)注释,并且会导致剃刀解析的问题,因为c#块中放置的一行注释如下所示:

@{
  ... 
  ...
  // anything
  ...
}

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.

将导致Razor解析器忽略行其余部分的缩小输出,从而产生一个错误,说明它找不到块的结束“}”。

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:

我解决这个问题的方法是从输出中完全删除这些注释。这样它的工作原理。为此,只需删除RegexOptions。从第145行单行模式:

htmlContents = Regex.Replace(htmlContents, @"//(.*?)\r?\n", ""/*, RegexOptions.Singleline*/);

#1


31  

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://omari-o.blogspot.com/2009/09/aspnet-white-space-cleaning-with-no.html。

#2


19  

Try HTTP module as described here: http://madskristensen.net/post/a-whitespace-removal-http-module-for-aspnet-20

尝试如下所示的HTTP模块:http://madskristensen.net/post/a- whitespacase -removal-http- moduleforaspnet -20

#3


9  

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:

对于Microsoft . net平台,有一个名为WebMarkupMin的库,它生成HTML代码的简化。对于每一个ASP。NET框架有自己的模块:

  • ASP.NET Core - WebMarkupMin.Web. Implemented as an HTTP module, so it can work with any framework. Suitable for use in the ASP.NET Web Pages framework (Razor).
  • ASP。网络核心——WebMarkupMin.Web。实现为HTTP模块,因此可以使用任何框架。适合在ASP中使用。NET网页框架(Razor)。
  • ASP.NET MVC - WebMarkupMin.Mvc
  • ASP。净MVC - WebMarkupMin.Mvc
  • ASP.NET Web Forms - WebMarkupMin.WebForms
  • ASP。网络表单- WebMarkupMin.WebForms。

Documentation is available at - http://webmarkupmin.codeplex.com/documentation

文档可以在http://webmarkupmin.codeplex.com/documentation中找到

#4


6  

I want to comment on Thorn's suggestion (but I'm new to stack overflow).

我想评论一下Thorn的建议(但我对stack overflow很陌生)。

  1. 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.

    链接代码(omari-o.blogspot.com)不支持MVC4,虽然代码是开源的,但由于MVC3和MVC4之间的刹车变化,它不能轻松升级。

  2. 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.

    在运行时,可能会有白空间写入http结果,只有实际站点的开发人员才能知道这一点。因此,模板文件(aspx)的静态缩小并不是万无一失的。gius建议使用动态缩小来保证白空间被正确删除,不幸的是,这将导致运行时计算成本。如果代码动态地向输出写入空格,则必须动态地删除它。

#5


3  

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

公认的答案不适用于MVC 4,所以这里有一个类似的库,可以在构建时使用https://github.com/jitbit/htmloptimimzervc4进行压缩

#6


2  

Just adding another option I do not see listed here, which is the one I was recommended using:

只是添加了另一个我在这里没有看到的选项,这是我建议使用的:

Html minifier command line tool

Html迷你器命令行工具。

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:

但是,这个工具有一个问题:它只留下一行(//)注释,并且会导致剃刀解析的问题,因为c#块中放置的一行注释如下所示:

@{
  ... 
  ...
  // anything
  ...
}

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.

将导致Razor解析器忽略行其余部分的缩小输出,从而产生一个错误,说明它找不到块的结束“}”。

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:

我解决这个问题的方法是从输出中完全删除这些注释。这样它的工作原理。为此,只需删除RegexOptions。从第145行单行模式:

htmlContents = Regex.Replace(htmlContents, @"//(.*?)\r?\n", ""/*, RegexOptions.Singleline*/);