Page页面文件,重新Render 方法,目的是把页面的ViewState信息放在最后,利于页面展示速度和SEO优化。
Render方法对于重新Html控件还是很好用的。
private static readonly Regex viewStateRegex = new Regex("<input type="hidden" name="__VIEWSTATE".*/>", RegexOptions.IgnoreCase); //过滤viewstate html标记的正则表达式 private static readonly Regex endFormRegex = new Regex(@"</form>", RegexOptions.Multiline | RegexOptions.Compiled); protected override void Render(HtmlTextWriter writer) { System.IO.StringWriter stringWriter = new System.IO.StringWriter(); HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter); base.Render(htmlWriter); string html = stringWriter.ToString(); Match viewStateMatch = viewStateRegex.Match(html); string viewStateString = viewStateMatch.Captures[0].Value;//找出ViewState的Html标记 目的把viewstate的内容放到页面FORM的最后面加快加载速度和搜索抓录 // viewStateString = "<input type=/"hidden/" name=/"__VIEWSTATE/" id=/"__VIEWSTATE/" value=/"/" />"; //仿博客园 html = html.Remove(viewStateMatch.Index, viewStateMatch.Length);//替换掉ViewState的html标记 Match endFormMath = endFormRegex.Match(html, viewStateMatch.Index); html = html.Insert(endFormMath.Index, viewStateString);//将ViewState的Html标记插入到</form>标记之前 writer.Write(html); }
做一头勤恳的技术牛