如何利用HttpModule修改文件内容

cnw168 2010-09-15 08:19:30
context.PreSendRequestContent += new EventHandler(this.fanghuoqing);

PreSendRequestContent 在ASP.NET把响应内容发送到客户端之前引发这个事件。这个事件允许我们在内容到达客户端之前改变响应内容。我们可以使用这个事件给页面输出添加用于所有页面的内容。例如通用菜单、头信息或脚信息。

资料上是这么说的但没有说具体方法.使用content.Response.Write到是可以统一添加内容,但只能是在输出内容的下面添加.

我想对输出的内容进行修改,比如替换一些文字,或将输出内容保存到硬盘上.但是在PreSendRequestContent 中不知道该如何读取输入内容.请指点.
...全文
171 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
newdigitime 2010-09-15
  • 打赏
  • 举报
回复
重载 render()事件处理程序就可以满足你的要求.

其它一些要求,譬如头信息,完全可以放一个服务器控件,然后用代码赋值.

当然你用自定义httpmodule模块也可以.
方法如楼上,继承 IHttpModule
wuyq11 2010-09-15
  • 打赏
  • 举报
回复
public void Init(HttpApplication context)
{
context.PreSendRequestContent += new EventHandler(context_PreRequestHandlerExecute);
}

void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
string url = context.Request.RawUrl;
}
直接StringWriter wr = new StringWriter();
Server.Execute("", wr);
this.lit.Text = Server.HtmlEncode(wr.ToString());
File.WriteAllText(Server.MapPath(""), wr.ToString());

别样苍茫 2010-09-15
  • 打赏
  • 举报
回复

using System;
using System.Web;

namespace xumh
{
/// <summary>
/// 多个HttpModule的应用示例:
/// web.config文件中HttpModules里面先Add哪一个他就先执行,按add的顺序分别执行
/// 多个HttpModule要过滤的事件会分别处理
/// </summary>
public class multiHttpModule : IHttpModule
{
public void Dispose()
{
throw new NotImplementedException();
}

public void Init(HttpApplication context)
{//重新定义ASP.NET的事件处理程序
context.BeginRequest += new EventHandler(myBeginRequest);
context.EndRequest += new EventHandler(myEndRequest);
context.PreRequestHandlerExecute += new EventHandler(myPreRequestHandlerExecute);
context.PostRequestHandlerExecute += new EventHandler(myPostRequestHandlerExecute);
context.ReleaseRequestState += new EventHandler(myReleaseRequestState);
context.AcquireRequestState += new EventHandler(myAcquireRequestState);
context.AuthenticateRequest += new EventHandler(myAuthenticateRequest);
context.AuthorizeRequest += new EventHandler(myAuthorizeRequest);
context.ResolveRequestCache += new EventHandler(myResolveRequestCache);
context.PreSendRequestHeaders += new EventHandler(myPreSendRequestHeaders);
context.PreSendRequestContent += new EventHandler(myPreSendRequestContent);

}

void myPreSendRequestContent(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
context.Response.Write("myPreSendRequestContent<br/>");
}

void myPreSendRequestHeaders(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
context.Response.Write("myPreSendRequestHeaders<br/>");
}

void myResolveRequestCache(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
context.Response.Write("myResolveRequestCache<br/>");
}

void myAuthorizeRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
context.Response.Write("BeginRequest<br/>");
}

void myAuthenticateRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
context.Response.Write("BeginRequest<br/>");
}

void myAcquireRequestState(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
context.Response.Write("myAcquireRequestState<br/>");
}

void myReleaseRequestState(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
context.Response.Write("myReleaseRequestState<br/>");
}

void myPostRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
context.Response.Write("myPostRequestHandlerExecute<br/>");
}

void myPreRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
context.Response.Write("myPreRequestHandlerExecute<br/>");
}

void myEndRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
context.Response.Write("myEndRequest<br/>");
}

void myBeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
context.Response.Write("multiHttpModules:myBeginRequest<br/>");
}

}
}
/*--===------------------------------------------===---
输出结果:
testHttpModule:myBeginRequest
multiHttpModules:myBeginRequest
BeginRequest
BeginRequest
myResolveRequestCache
myAcquireRequestState
myPreRequestHandlerExecute


myPostRequestHandlerExecute
myReleaseRequestState
myEndRequest
myPreSendRequestHeaders
--===------------------------------------------===---*/

62,272

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧