Saturday, 31 August 2013

ASP.NET CUSTOM ERROR PAGE

                  ASP.NET CUSTOM ERROR PAGE


There is simple TWO step to create custom error in asp.net using c#

1- create a error.aspx and coding in error.aspx.cs
2- create a Global.asax page and coding in this page inside Application_Error event.


stept 1- error.aspx

  <h4 id="errordetail" style="color: #CC3300" runat="server"></h4>
                            <asp:Literal ID="lit" runat="server"></asp:Literal><br />                            
                            <h4 style="color: #CC3300">Error - Please contact to administrator. </h4>

step 2- error.aspx.cs
protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string strERef = Request.QueryString["ERef"];
            //Create Exception object from application variable
            Exception oException = (Exception)HttpContext.Current.Application[strERef];
            if (HttpContext.Current.IsDebuggingEnabled == true)
            {
                if (oException.InnerException.Message != null)
                {
                    errordetail.InnerText = oException.InnerException.Message.ToString();
                    lit.Text = oException.StackTrace.ToString();
                }
            }
            //Remove application variable
            HttpContext.Current.Application.Remove(strERef);
        }
        catch (Exception ex)
        {

        }
    }
    protected void Page_PreInit(object sender, EventArgs e)
    {
        if (Request.QueryString["aspxerrorpath"] != null)
        {
            if (Request.QueryString["aspxerrorpath"].Contains("Modal"))
            {
                this.MasterPageFile = "~/MasterPage/TelerikModel.master";
            }
        }
    }

step 3- global.asax


void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs
       // Server.Transfer("~/Error.aspx", true);

        string strERef = Guid.NewGuid().ToString();
        Exception objError = Server.GetLastError();
        Application.Add(strERef, objError);
        Server.ClearError();
        Response.Redirect(string.Format("~/Error.aspx?aspxerrorpath={0}&ERef={1}", HttpUtility.UrlEncode(Request.RawUrl), strERef), true);
    }

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home