HttpModule - 获取 HTML 内容或控件进行修改

发布于 2024-07-10 04:02:13 字数 449 浏览 9 评论 0原文

尝试了这样的事情:

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

Tried something like this:

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);    

when running this I get "Object reference not set to an instance of an object." for the last line...

How do I get to insert two lines of text (asp.net/html) at specific loactions in the original file?
And how do I figure out the extension of the file (I only want to apply this on aspx files...?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清晨说晚安 2024-07-17 04:02:13

它比您想象的更简单:

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

Its simplier than you think:

    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...
        }
    }

If the PreRender Event isn't sufficient u can add whatever Event u need in the PreRequestHandlerExecute EventHandler...

蒲公英的约定 2024-07-17 04:02:13

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

I'm not sure, but I don't think you can use an HttpModule to alter the Page's control tree (please correct me if I'm wrong). You CAN modify the HTML markup however, you'll have to write a "response filter" for this. For an example, see http://aspnetresources.com/articles/HttpFilters.aspx, or google for "httpmodule response filter".

陈年往事 2024-07-17 04:02:13

看起来 HttpFilter 解决方案在这里发挥了作用:o)

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

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

It seems like the HttpFilter solution is doing the trick here :o)

If I had used MOSS/.net 2.x+ I could have used Runes version or just added my tags in a master page...

Super suggestions and after my test of the solution, I'll accept miies.myopenid.com's solution as it seems to solve thar actual issue

夏尔 2024-07-17 04:02:13

与 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 文件中您想要自定义内容的任何位置。

There have been some changes in how you write HttpModules in IIS7 as compared to IIS6 or 5, so it might be that my suggestion is not valid if you are using IIS7.

If you use the Current static property of the HttpContext you can get a reference to the current context. The HttpContext class has properties for both the Request (HttpRequest type) and the Response (HttpResponse) and depending on where which event you are handling (Application.EndRequest maybe?) you can perform various actions on these objects.

If you want to change the content of the page being delivered you will probably want to do this as late as possible so responding to the EndRequest event is probably the best place to do this.

Checking which file type that was requested can be done by checking the Request.Url property, maybe together with the System.IO.Path class. Try something like this:

string requestPath = HttpContext.Current.Request.Url.AbsolutePath;
string extension = System.IO.Path.GetExtension(requestPath);
bool isAspx = extension.Equals(".aspx");

Modifying the content is harder. You may be able to do it in one of the events of the Context object, but I am not sure.

One possible approach could be to write your own cusom Page derived class that would check for a value in the Context.Items collection. If this value was found you could add a Label to a PlaceHolder object and set the text of the label to whatever you wanted.

Something like this should work:

Add the following code to a HttpModule derived class:

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";
  }

}

Then you add the following class to the App_Code folder:

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);

  }

}

Then you you modify your pages like this:

public partial class _Default : CustomPage

Note that the inheritance is changed from System.Web.UI.Page to CustomPage.

And finally you add PlaceHolder objects to your aspx files wherever you want you custom content.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文