asp.net 使用HttpModule对全站输出的动态页面的HTML内容进行修改,不会错乱
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了asp.net 使用HttpModule对全站输出的动态页面的HTML内容进行修改,不会错乱,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2973字,纯文字阅读大概需要5分钟。
内容图文
![asp.net 使用HttpModule对全站输出的动态页面的HTML内容进行修改,不会错乱](/upload/InfoBanner/zyjiaocheng/1111/9f7bdaade5134cd982356ec4dcae0163.jpg)
比上一编《asp.net 使用页适配器和重写Render对全站输出的页面的HTML内容进行修改,不会错乱》更容易的方方
http://blog.csdn.net/lrxin/article/details/40861039
配置方法:
<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; } }
原文:http://blog.csdn.net/lrxin/article/details/43968683
内容总结
以上是互联网集市为您收集整理的asp.net 使用HttpModule对全站输出的动态页面的HTML内容进行修改,不会错乱全部内容,希望文章能够帮你解决asp.net 使用HttpModule对全站输出的动态页面的HTML内容进行修改,不会错乱所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。