3

尝试过这样的事情:

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 文件......?

4

4 回答 4

6

它比你想象的要简单:

    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 中添加您需要的任何事件...

于 2010-06-04T14:31:55.027 回答
4

我不确定,但我认为您不能使用 HttpModule 来更改页面的控制树(如果我错了,请纠正我)。您可以修改 HTML 标记,但是,您必须为此编写一个“响应过滤器”。例如,请参阅http://aspnetresources.com/articles/HttpFilters.aspx或 google 中的“httpmodule 响应过滤器”。

于 2008-12-02T08:37:07.403 回答
1

似乎 HttpFilter 解决方案在这里起到了作用:o)

如果我使用过 MOSS/.net 2.x+,我可以使用 Runes 版本或者只是在母版页中添加我的标签......

超级建议,在我测试解决方案后,我会接受 miies.myopenid.com 的解决方案,因为它似乎解决了实际问题

于 2008-12-03T07:34:28.230 回答
0

与 IIS6 或 5 相比,您在 IIS7 中编写 HttpModules 的方式发生了一些变化,因此如果您使用的是 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 对象的事件之一中做到这一点,但我不确定。

一种可能的方法是编写您自己的自定义页面派生类,该类将检查 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 文件中。

于 2008-12-02T09:21:59.903 回答