ASP.NET代码对页面输出进行清理

好的代码,自然要保存:

代码
private   static   readonly  Regex REGEX_LINE_BREAKS  =   new  Regex( @" \n\s* " , RegexOptions.Compiled);
private   static   readonly  Regex REGEX_LINE_SPACE  =   new  Regex( @" \n\s*\r " , RegexOptions.Compiled);
private   static   readonly  Regex REGEX_SPACE  =   new  Regex( @" ( )+ " , RegexOptions.Compiled);

protected   override   void  Render(HtmlTextWriter writer)
{
    
using  (HtmlTextWriter htmlwriter  =   new  HtmlTextWriter( new  System.IO.StringWriter()))
    {
        
base .Render(htmlwriter);
        
string  html  =  htmlwriter.InnerWriter.ToString();
        html 
=  REGEX_LINE_BREAKS.Replace(html,  string .Empty);
        html 
=  REGEX_LINE_SPACE.Replace(html,  string .Empty);
        html 
=  REGEX_SPACE.Replace(html,  "   " );
        writer.Write(html.Trim());
    }
}

 

 

你可能感兴趣的:(asp.net)