asp.net頁面過濾所有分行符號和多餘空格

來源:互聯網
上載者:User

標籤:blog   os   io   re   c   html   

不知道大家注意到了沒有,Google和Baidu網頁的HTML原始碼是混合在一起的。HTML代碼混合在一起,出發點是為了減小網頁體積,從而加快網頁載入速度。

      寫個函數把網頁HTML原始碼的分行符號和空格過濾掉其實並不難,我這裡是寫了個基類,在asp.net編程時,頁面只要繼承這個基類,那麼輸出的HTML代碼就會自動去掉分行符號,和多餘的空格符號,例如“> <”之間的空格符號。

using System;  
using System.Data;  
using System.Configuration;  
using System.Web; 
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Web.UI.HtmlControls; 
using System.Text.RegularExpressions;  
using System.IO;   
/// <summary>  
/// PageBase 頁面基類 
/// </summary>  
public class PageBase : System.Web.UI.Page  
{      
 protected override void Render(HtmlTextWriter writer)      
 {         
  StringWriter sw = new StringWriter();          
  HtmlTextWriter htmlWriter = new HtmlTextWriter(sw);          
  base.Render(htmlWriter);          
  string html = sw.ToString();          
  html = Regex.Replace(html, "[\f\n\r\t\v]", "");          
  html = Regex.Replace(html, " {2,}", " ");          
  html = Regex.Replace(html, ">[ ]{1}", ">");          
  writer.Write(html);      
 }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.