Centre for Software Practice

the blog
Welcome to Centre for Software Practice Sign in | Join | Help
in Search

Tim's blog

  • Good Error Reporting for ASP.Net Websites with MS-AJAX

    A requirement in many ASP.Net web applications out there is that you the developer know when your website breaks, the usual way was to use the Application_Error event and then send your self an email or write to a log file.  With the event of AJAX itergration into ASP.Net v2.0 the Application_Error event does not run when the request was an Asyncronous PostBack, fear not this is a fixable problem.  This article is will cover my personal opinion on a good practice method of error sending. Firstly i will breifly go over the method to run the Application_Error event.

    First Step go to the root of the website and add a new item, add the Global Application Class File leave the file name alone then click Add. you should now have a file Called Global.asax in the root of your website. you can make the Global.asax use a code behind file, which i would recomend.  The code for error handling looks something like this:

    Global.asax:

    <%@ Application Inherits="CSP.Global" Language="C#" %>

    App_Code/Global.asax.cs

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Net.Mail;

    namespace CSP
    {
        /// <summary>
        /// Summary description for Global
        /// </summary>
        public class Global : System.Web.HttpApplication
        {
            void Application_Error(object sender, EventArgs e)
            {
              // Code that runs when an unhandled error occurs
              ExceptionHandling.SendException(Server.GetLastError(), HttpContext.Current);
            }

        }
    }

    This Code runs a function that sends an error message email to the developers in charge of the application, where the first argument is of type Exception and the second of type HttpContext.  The HttpContext.Current object can give us information about the request that caused the error, the session state when it happened, the user who caused the error (via Session state, cookie or IP address, etc) and more.

    On to AJAX exception handling. To my surprise the when an error occured during an AJAX postback the above code never got run so i had to track down what the AJAX alternative was.  It was quite simple to find it turned out, the error event seems to be handled by the ScriptManger Control which oddly enough has a OnAsyncPostBackError Event.  The point of this post was not my "grand" discovery but rather the best practice method, so back to that.  The best way to handle this event was to make sure it was centrally handled once a site was setup and didn't need to be worried about on any subsequent new pages therefore it was perfect to put this into the Master Page.

     In the master page put a scriptmanager in the document body inside the server side form tags like so:

            <ajax:ScriptManager ID="ScriptManagerMain" runat="server" EnablePartialRendering="true"
                OnAsyncPostBackError="ScriptManagerMain_AsyncPostBackError">
            </ajax:ScriptManager>

    The code to run the error should look something like this:

        protected void ScriptManagerMain_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
        {
            CSP.ExceptionHandling.SendException(e.Exception, HttpContext.Current);
        }
     

    Note the event arguments for this event are custom and you can get the Exception Error from them.

    If your website has a combination of these 2 error catching methods you can rest assured that your application is working smoothly whilst running AJAX and that all errors are being reported.

  • IE only Stylesheets in ASP websites

    a very simple feature i implemented today was to make an IE only style sheet for my application i'm writing it only took 2 lines to make it work.  Firstly put the following line in your asp page (best place would be a master page):

    <link href="Styles/IEStyle.css" rel="stylesheet" type="text/css" runat="server" id="lnkIEStyle" visible="false" /> 

    this should go after any other style sheets so that it's content takes precedence over the other style sheets.

    Secondly put the following line of code (it is in C#) in your page load:

    lnkIEStyle.Visible = Request.UserAgent.Contains("MSIE");

    this works very well and you can easily make your website look the same across numerous browsers and the method could be used for different browser detection such as if the user agent string contains "gecko" you could single out mozilla based browsers.

  • Bugzilla the Pest

    where is my flyspray ...

    Bugzilla does not have the greatest support when the computer you are serving it from dies and you need to change it over to another.  this annoying fact cost far too much time. on the upside we got it working using a new url bugs.csp.uwa.edu.au so there was some benefit from the death of the last computer.

    and while we're at it what is with cabage prices in New Zealand :) ....... 

Powered by Community Server (Personal Edition), by Telligent Systems