HttpModule - Get HTML content or controls for modifications

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 I run this, I get "The object reference is not set to the object instance." for the last line ...

How do I get an insert of two lines of text (asp.net/html) with certain changes in the source file? And how can I determine the file extension (I want to apply this only to aspx files ...?

+3
source share
4 answers

, , HttpModule (, , ). HTML, " ". , . http://aspnetresources.com/articles/HttpFilters.aspx google " httpmodule".

+4

, :

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

+6

, HttpFilter : o)

MOSS/.net 2.x +, Runes ...

- , miies.myopenid.com, , , .

+1

, HttpModules IIS7 IIS6 5, , , IIS7.

HttpContext, . HttpContext ( HttpRequest), Response (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, .

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

0

All Articles