Question

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

Was it helpful?

Solution

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

OTHER TIPS

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

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top