HttpModule - get HTML content or controls for modifications

前端 未结 4 820
长情又很酷
长情又很酷 2021-01-03 09:02

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


        
相关标签:
4条回答
  • 2021-01-03 09:08

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

    0 讨论(0)
  • 2021-01-03 09:25

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

    0 讨论(0)
  • 2021-01-03 09:25

    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

    0 讨论(0)
  • 2021-01-03 09:30

    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.

    0 讨论(0)
提交回复
热议问题