override render 方法
2013-09-02 18:15 Spring.Guo 阅读(900) 评论(0) 编辑 收藏 举报有时候需要在ASP.net 或MVC 中在页面呈现前,把要显示的内容作一个拦截,更改内容后显示。
只要重写
protected override void Render(System.Web.UI.HtmlTextWriter writer) 方法。
例如: 我把返回的内容中的html 的标题进行改变,可以如下操作
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
StringBuilder sb = new StringBuilder();
HtmlTextWriter tw = new HtmlTextWriter(new System.IO.StringWriter(sb));
//Render the page to the new HtmlTextWriter which actually writes to the stringbuilder
base.Render(tw);
//Get the rendered content
string sContent = sb.ToString();
//set title
sContent = Regex.Replace(sContent, @"(?<=title>)(.*?)(?=</title>)","我的标题");
//Now output it to the page, if you want
writer.Write(sContent);
}
感谢google,再一次BS baidu