locked
Accessing aspx page label control from httpmodule RRS feed

  • Question

  • User-910777465 posted

    Hi,

    I have this httpmodule for handling exceptions. When an error occurs, this module will email the exception details to the website admin user, then redirects to an error page, say error.aspx. I have a label control in error.aspx. I would like to write out user friendly error details in this label control from the httpmodule. Can it be done? 

    Thanks. 

     

    Monday, June 25, 2007 2:04 PM

Answers

  • User-900677162 posted

    HI, mxitiup:

    As far as i know, it can not be done. HttpModules As requests move through the pipeline a number of events fire on the HttpApplication object. Modules are in essence filters - similar in functionality to ISAPI filters at the ASP.NET request level. Modules allow hooking events for EVERY request that pass through the ASP.NET HttpApplication object. By implementing specific interfaces and methods the module then gets hooked up to the HttpApplication event chain.

    However the page instance is generated after the HttpModule fires. SO  i think you can not get the reference to the page in the HttpModule period.

    More details you can check out this article about:

    A low-level Look at the ASP.NET Architecture

    http://www.programmersheaven.com/2/a-low-level-look-at-the-asp-page-3

    I hope the above information will be helpful. If you have any issues or concerns, please let me know. It's my pleasure to be of assistance

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 27, 2007 9:45 PM

All replies

  • User-1583677305 posted

    You can use reflection (FieldInfo class) or add public property ErrorMessage into error.aspx page:

    public partial class error : System.Web.UI.Page
    {
        public string ErrorMessage
        {
            set { label.Text = value; }
        }
    }
    
    Monday, June 25, 2007 5:30 PM
  • User-1583677305 posted

    Now I have next idea, I think this will be most elegant solution. In HttpModule place into HttpContext.Current.Items.Add("errmsg", "some text for label, ....") and in error.aspx codebehind write label.Text = HttpContext.Current.Items["errmsg"];

    Monday, June 25, 2007 5:36 PM
  • User-910777465 posted

    Hi,

    I tried adding items to the httpcontext and retrieving it form the error page, it is not working. In the httpmodule I have this line,

                HttpContext.Current.Items.Add("ErrorMsg", "Test HTTPContext Items");

    In error.aspx I have this,

                if (HttpContext.Current.Items["ErrorMsg"] != null)
                {
                    LblError.Text = HttpContext.Current.Items["ErrorMsg"].ToString();                   
                }

    The condition always fails, the value is always null. Am I doing something wrong? May be its because I am doing a Reponse.Redirect to error.aspx?

    Tuesday, June 26, 2007 10:15 AM
  • User-910777465 posted

    If I have a public property in error.aspx page, how can I set its value from the httpmodule? Using Context.Handler?

    Thanks. 

    Tuesday, June 26, 2007 10:16 AM
  • User-900677162 posted

    HI, mxitiup   

    An Error Module

    One of the coolest new application events in ASP.NET is the Error event. As mentioned before with an HttpModule you can sink this event in an application-specific way in Global.asax. You can redirect the user away from the error page to some other part of the site that is more appropriate than just an error message. It might be interesting to sink the error event in a module, however, to provide a non-application-specific piece of functionality. A common idea is to log the error to the event log for later analysis or perhaps even to e-mail it to the Web master. This can be done in an application-independent way, which indicates the need for an HttpModule.

    The following sample shows an HttpModule that logs the error information to an event log and e-mails the Webmaster with the error information. It first attempts to connect to an event log called "ASP.NET ErrorModule," which is created if it doesn't already exist. Next, it gathers the error information from the HttpApplication.Context.Error property. This property returns the exception that was thrown during the processing of this request. Several of the Exception properties are bundled into a string, which is then logged to the event log. Finally, the error is sent to the Webmaster using the SmtpMailClass.

    public class ErrorModule : IHttpModule
    {
      private const string strEVENTLOGNAME = "ASP.NET ErrorModule";

      private HttpApplication mApplication;

      public void Init(HttpApplication application)
      {
        // Save off the application
        mApplication = application;

        // Wire up the error handler
        mApplication.Error += new System.EventHandler(this.ErrorHandler);
      }

      private void ErrorHandler(object sender, EventArgs e)
      {
        // Create the event source if it doesn't exist
        if(!EventLog.SourceExists(strEVENTLOGNAME))
          EventLog.CreateEventSource(strEVENTLOGNAME, strEVENTLOGNAME + " Log");

        // Create an event log instance and point it at the event source
        EventLog el = new EventLog();
        el.Source = strEVENTLOGNAME;

        // Create the error text
        string strErrorMessage = "An uncaught exception was thrown in your application\r\nUrl: " +
    mApplication.Request.Url.ToString() + "\r\nMessage:" + mApplication.Context.Error.Message + "\r\nStack
    Trace:" + mApplication.Context.Error.StackTrace;

        // Write the event log entry
        el.WriteEntry(strErrorMessage, EventLogEntryType.Error);

        // Mail the message to the web master
        System.Web.Mail.SmtpMail.Send("webserver@vergentsoftware.com", "ckinsman@vergentsoftware.com",
    "Web Site Error", strErrorMessage);
      }

      public void Dispose()
      {
      }
    }

    http://devforum/aspnetforumreporttool/report.aspx?ID=1008

    Tuesday, June 26, 2007 10:09 PM
  • User-910777465 posted

    Hi,

    Thanks for the sample code. I already have setup the module to send an email to the administrator. I will add the event logging ability as well.

    Any suggestion on how to set the label control text property from the httpmodule?

    Thanks. 

    Wednesday, June 27, 2007 10:09 AM
  • User-900677162 posted

    HI, mxitiup:

    As far as i know, it can not be done. HttpModules As requests move through the pipeline a number of events fire on the HttpApplication object. Modules are in essence filters - similar in functionality to ISAPI filters at the ASP.NET request level. Modules allow hooking events for EVERY request that pass through the ASP.NET HttpApplication object. By implementing specific interfaces and methods the module then gets hooked up to the HttpApplication event chain.

    However the page instance is generated after the HttpModule fires. SO  i think you can not get the reference to the page in the HttpModule period.

    More details you can check out this article about:

    A low-level Look at the ASP.NET Architecture

    http://www.programmersheaven.com/2/a-low-level-look-at-the-asp-page-3

    I hope the above information will be helpful. If you have any issues or concerns, please let me know. It's my pleasure to be of assistance

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 27, 2007 9:45 PM