去掉asp.net自动生成的垃圾代码的一个类,

时间:2020-11-28 09:06:38

using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;
using System.Web.UI;

/// <summary>
/// Summary description for StaticPageInfo
/// </summary>
public class StaticPageInfo : System.Web.UI.Page
{
    protected override void Render(HtmlTextWriter writer)
    {
        StringWriter sw = new StringWriter();

        base.Render(new HtmlTextWriter(sw));

        string html = sw.ToString();
        html = ReplaceView(html);
        writer.WriteLine(html.Trim());
    }
    public string ReplaceView(string BodyHtml)
    {
        BodyHtml = Regex.Replace(BodyHtml, @"<form([\s|\S]*?)>([\s|\S]*?)</form>", @"$2", RegexOptions.IgnoreCase);
        BodyHtml = Regex.Replace(BodyHtml, @"<input type=""hidden"" name=""__VIEWSTATE""([\s|\S]*?)/>", @"", RegexOptions.IgnoreCase);
        BodyHtml = Regex.Replace(BodyHtml, @"<input type=""hidden"" name=""__EVENTVALIDATION""([\s|\S]*?)/>", @"", RegexOptions.IgnoreCase);
        BodyHtml = Regex.Replace(BodyHtml, @"<form([\s|\S]*?)>([\s|\S]*?)</form>", @"$2", RegexOptions.IgnoreCase);
 
        return BodyHtml;
    }
}