views:

2907

answers:

4

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

+3  A: 

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

you're probably perfectly right on the matter of control tree, thats jut my ignorance on the subject. I'll look into the http filter stuff
noesgard
even if this thread is pretty old, its possible just look at my post.
haze4real
A: 

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.

Rune Grimstad
I'm writing .net 1.1 compliant code, since this is to be run on a SPS 2003. there will not be any conflicts with IIS 7 and I cant use partial classes or other .net 2.0+ technologiesWill take a look and see if I can get this to work
noesgard
I tested it under .NET 3.5, but it should work properly on 1.1 as well I think. Good luck!
Rune Grimstad
adding custom code to each page is not an option here... only if I can do it dynamically... I'm trying to avoid manual edit of 2700 files!
noesgard
detecting the file type works like a charm... Am trying to work out the main issue now (trying to insert ekstra HTML i a file without adding new placeholders etc.)
noesgard
Where in the document are you adding the html? Is it a fixed place in the document or do all pages have a control there?
Rune Grimstad
I'm working on it now and I have to add a rather large piece of JavaScript (which initially was a usercontrol) and I'm now working with the Filter and HttpModule solution, which for now seems to work :oD
noesgard
+1  A: 

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

noesgard
+1  A: 

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

haze4real