您的位置:首页 > 理论基础 > 计算机网络

asp.net 使用HttpModule对全站输出的动态页面的HTML内容进行修改,不会错乱

2015-02-27 15:58 661 查看
比上一编《asp.net 使用页适配器和重写Render对全站输出的页面的HTML内容进行修改,不会错乱》更容易的方方

/article/10681672.html

配置方法:

<httpModules>

<add name="FileEditModule" type="Framework.FileEditModule, Framework" />

</httpModules>

<pre name="code" class="csharp">public class FileEditModule : System.Web.IHttpModule
{
public void Dispose() { }

public void Init(HttpApplication application)
{
application.EndRequest += new EventHandler(application_EndRequest);
}

void application_EndRequest(object sender, EventArgs e)
{
try
{
HttpApplication application = (HttpApplication)sender;

Uri url = application.Request.Url;
// 如果请求Url需要验证
string[] ss = application.Request.FilePath.Split('.');
string extension = ss[ss.Length - 1];
extension = extension.ToLower();
string[] extensions = new string[] {
"ashx",
"aspx",
"asmx"
};
if (extensions.Contains(extension))
{
string html = GetRenderHtml(null, application.Response);
//....对html进行操作
application.Response.ClearContent();
application.Response.Write(html);

}
}
catch (Exception ex) { }
}
public string GetRenderHtml(HtmlTextWriter writer, HttpResponse Response)
{
//var Response = this.Page.Response;

//Response.Output其实就是一个HttpWriter,Response.OutputStream其实就是HttpResponseStream,Object.ReferenceEquals (Response.Output,(Response.OutputStream as HttpResponseStream)._writer)为true

BindingFlags bind = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.GetField;
//因为HttpWriter._charBuffer这个字符数组的长度是1024,
//所以推测,一旦某一段字符串超过1024就会创建一个IHttpResponseElement,
//然后加入到HttpWriter._buffers,HttpWriter._buffers的每一个元素都实现了IHttpResponseElement接口,
//具体类型可能是HttpResponseUnmanagedBufferElement,HttpSubstBlockResponseElement等类型
ArrayList arr = (ArrayList)Response.Output.GetType().GetField("_buffers", bind).GetValue(Response.Output);

Assembly systemWeb = Assembly.Load("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
Type type = systemWeb.GetType("System.Web.IHttpResponseElement");
MethodInfo method = type.GetMethod("GetBytes");
StringBuilder sb = new StringBuilder(5000);
//遍历每一个buffer,获取buffer里存储的字符数组,然后转换为字符串
for (int i = 0; i < arr.Count; i++)
{
byte[] buffer = (byte[])method.Invoke(arr[i], null);
//使用当前编码得出已经存储到HttpWriter._buffers中的字符串
sb.Append(Response.ContentEncoding.GetString(buffer));
}
//获取HttpWriter的字符数组缓冲区
char[] charBuffer = (char[])Response.Output.GetType().GetField("_charBuffer", bind).GetValue(Response.Output);
int charBufferLength = (int)Response.Output.GetType().GetField("_charBufferLength", bind).GetValue(Response.Output);
int charBufferFree = (int)Response.Output.GetType().GetField("_charBufferFree", bind).GetValue(Response.Output);
//charBufferLength - charBufferFree 等于字符数组缓冲区已经使用的字符数量
for (int i = 0; i < charBufferLength - charBufferFree; i++)
{
sb.Append(charBuffer[i]);
}
string html = sb.ToString();
return html;
}
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐