HttpModule - 获取 HTML 内容或控件进行修改
-
22-07-2019 - |
题
尝试过这样的事情:
HttpApplication app = s as HttpApplication; //s is sender of the OnBeginRequest event
System.Web.UI.Page p = (System.Web.UI.Page)app.Context.Handler;
System.Web.UI.WebControls.Label lbl = new System.Web.UI.WebControls.Label();
lbl.Text = "TEST TEST TEST";
p.Controls.Add(lbl);
运行此功能时,我将获得“对象引用未设置为对象的实例”。对于最后一行...
如何在原始文件的特定位置插入两行文本(asp.net/html)?我如何找出文件的扩展名(我只想将其应用于 aspx 文件......?
解决方案
我不确定,但我认为您不能使用 HttpModule 来更改页面的控制树(如果我错了,请纠正我)。您可以修改 HTML 标记,但是,您必须为此编写一个“响应过滤器”。有关示例,请参见 http://aspnetresources.com/articles/HttpFilters.aspx, ,或谷歌搜索“httpmodule响应过滤器”。
其他提示
它比你想象的更简单:
public void Init(HttpApplication app)
{
app.PreRequestHandlerExecute += OnPreRequestHandlerExecute;
}
private void OnPreRequestHandlerExecute(object sender, EventArgs args)
{
HttpApplication app = sender as HttpApplication;
if (app != null)
{
Page page = app.Context.Handler as Page;
if (page != null)
{
page.PreRender += OnPreRender;
}
}
}
private void OnPreRender(object sender, EventArgs args)
{
Page page = sender as Page;
if (page != null)
{
page.Controls.Clear(); // Or do whatever u want with ur page...
}
}
如果 PreRender 事件不够,您可以在 PreRequestHandlerExecute EventHandler 中添加您需要的任何事件...
看起来 HttpFilter 解决方案在这里起作用了:o)
如果我使用 MOSS/.net 2.x+,我可以使用 Runes 版本或者只是在母版页中添加我的标签...
超级建议,在我测试解决方案后,我会接受 miies.myopenid.com 的解决方案,因为它似乎解决了实际问题
与 IIS6 或 5 相比,在 IIS7 中编写 HttpModule 的方式发生了一些变化,因此如果您使用 IIS7,我的建议可能无效。
如果使用 HttpContext 的 Current 静态属性,您可以获得对当前上下文的引用。HttpContext 类具有请求(HttpRequest 类型)和响应(HttpResponse)的属性,并且根据您正在处理哪个事件(也许是 Application.EndRequest?),您可以对这些对象执行各种操作。
如果您想要更改正在传递的页面的内容,您可能会希望尽可能晚地执行此操作,因此响应 EndRequest 事件可能是执行此操作的最佳位置。
检查所请求的文件类型可以通过检查 Request.Url 属性来完成,也可以与 System.IO.Path 类一起检查。尝试这样的事情:
string requestPath = HttpContext.Current.Request.Url.AbsolutePath;
string extension = System.IO.Path.GetExtension(requestPath);
bool isAspx = extension.Equals(".aspx");
修改内容比较困难。您也许可以在 Context 对象的事件之一中执行此操作,但我不确定。
一种可能的方法是编写您自己的自定义 Page 派生类,该类将检查 Context.Items 集合中的值。如果找到此值,您可以将标签添加到 PlaceHolder 对象,并将标签的文本设置为您想要的任何内容。
像这样的东西应该有效:
将以下代码添加到 HttpModule 派生类:
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(BeginRequest);
}
void BeginRequest(object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
HttpRequest request = context.Request;
string requestPath = HttpContext.Current.Request.Url.AbsolutePath;
string extension = System.IO.Path.GetExtension(requestPath);
bool isAspx = extension.Equals(".aspx");
if (isAspx)
{
// Add whatever you need of custom logic for adding the content here
context.Items["custom"] = "anything here";
}
}
然后将以下类添加到 App_Code 文件夹中:
public class CustomPage : System.Web.UI.Page
{
public CustomPage()
{ }
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (Context.Items["custom"] == null)
{
return;
}
PlaceHolder placeHolder = this.FindControl("pp") as PlaceHolder;
if (placeHolder == null)
{
return;
}
Label addedContent = new Label();
addedContent.Text = Context.Items["custom"].ToString();
placeHolder .Controls.Add(addedContent);
}
}
然后你像这样修改你的页面:
public partial class _Default : CustomPage
请注意,继承从 System.Web.UI.Page 更改为 CustomPage。
最后,您可以将 PlaceHolder 对象添加到您的 aspx 文件中您想要自定义内容的任何位置。