asp.net 生成静态页时如何过滤掉viewstate

发布时间:2020-08-09编辑:脚本学堂
asp.net生成的静态文件,总会有一些viewstate的字符,本文提供的代码,可以帮助你去掉这些东西,因为是在html静态页面,保留它们是没有必要的。
专题教程:asp.net生成静态面页方法教程大全
复制代码 代码示例:

public static string GetSourceTextByUrl(string url)
{
WebRequest request = WebRequest.Create(url);
request.Timeout = 200000;//20秒超时
WebResponse response = request.GetResponse();
Stream resStream = response.GetResponseStream();
StreamReader sr = new StreamReader(resStream);
string tempstr = sr.ReadToEnd();

Regex r1 = new Regex("<input type="hidden" name="__EVENTTARGET".*/>", RegexOptions.IgnoreCase);
Regex r2 = new Regex("<input type="hidden" name="__EVENTARGUMENT".*/>", RegexOptions.IgnoreCase);
Regex r3 = new Regex("<input type="hidden" name="__VIEWSTATE".*/>", RegexOptions.IgnoreCase);
//过滤<form>代码
Regex r4 = new Regex("<form name="aspnetForm".*id="aspnetForm">", RegexOptions.IgnoreCase);
Regex r5 = new Regex("</form>");
tempstr = r1.Replace(tempstr, "");
tempstr = r2.Replace(tempstr, "");
tempstr = r3.Replace(tempstr, "");
tempstr = r4.Replace(tempstr, "");
tempstr = r5.Replace(tempstr, "");

return tempstr;
}