public class ResultFilterAttribute : ActionFilterAttribute
{
//用于保存渲染后的html文本
StringBuilder sb;
//這幾個Writer照著寫就行了
StringWriter sw;
HtmlTextWriter hw;
TextWriter tw;
/// <summary>
///加載 "視圖" 前執行
/// </summary>
/// <param name="filterContext"></param>
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
//保存html
sb = new StringBuilder();
//兩個writer
sw = new StringWriter(sb);
hw = new HtmlTextWriter(sw);
//記住Response中原本輸出流,用于回傳本次請求的html,與下一句配合使用
//在渲染結束后,向tw內寫入html內容
tw = filterContext.RequestContext.HttpContext.Response.Output;
//過濾器自己輸出流,用于獲取渲染后的html內容
filterContext.RequestContext.HttpContext.Response.Output = hw;
}
/// <summary>
/// 加載"視圖" 后執行
/// </summary>
/// <param name="filterContext"></param>
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
string html = sb.ToString();
//對頁面進行二次處理 在輸出新的html 寫入 filterContext.RequestContext.HttpContext.Response.Output
tw.Write(html);
}
}
問題:如果多次重繪頁面,會導致內容追加顯示別的HTML內容,或者是頁面內容無法顯示,顯示空白,請問如何處理并發,正常輸出頁面
uj5u.com熱心網友回復:
問題:如果多次重繪頁面,會導致內容追加顯示別的HTML內容,或者是頁面內容無法顯示,顯示空白,請問如何處理并發,正常輸出頁面uj5u.com熱心網友回復:
怎么一個大神都沒有????????????????????
uj5u.com熱心網友回復:
在OnActionExecuted中獲取頁面內容 修改輸出轉載請註明出處,本文鏈接:https://www.uj5u.com/net/135069.html
標籤:C#
