Filter asp.net blank spaces in the output HTML

Source: Internet
Author: User
asp.net

Just "accidentally" checked the asp.net generated code, surprised to find that the asp.net output of the code is so not compact, in the label there are countless spaces and tabs (tab), especially with a nested table, the scene is really miserable ah, there are pictures as evidence:


Supposedly adding so many useless spaces in the code generated by ASP.net/tab may be to make the generated HTML code more readable, but the resulting problem is to make the output code larger, in the limited bandwidth today is a crime, waste the user's money, affect the user's experience! And ASP.net is a server-side programming, it is not possible to directly edit the resulting HTML code, even if all the space/tab have gone, will not bring any bad impact!?

Based on the above considerations, the sacrifice of reflector, it was found that HtmlTextWriter in the indent attributes control the size of the indentation, and fortunately this property or public type, really help me also, spent a few minutes, the following code:
1 public class Filterhtmltextwriter:htmltextwriter
2 {
3 Public Filterhtmltextwriter (TextWriter writer, string tabstring)
4:base (writer, tabstring)
5 {
6}
7
8 Public new int Indent
9 {
Ten get {return 0;} Always return 0
One set {base. Indent = 0; //Always set 0
12}
13}
Then overload the page's Render method (better if all pages have a common base class), add a line of code, as follows:
1 public partial class _default:system.web.ui.page
2 {
3 protected override void Render (HtmlTextWriter writer)
4 {
5//Replace writer for our custom class
6 writer = new Filterhtmltextwriter (writer. Innerwriter, "");
7 base. Render (writer);
8}
9//
10}
At this point, let us look at the output of the page again, wow, the annoying space has become less @_@, as shown in the following figure:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.