[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 317
  • Last Modified:

Custom Error Messages

Good Evening Folks!
I would like to write my own custom error page with our company logo rather then the standard. Another words if the page doesnt exsist , redirect to my own page. If this can be done how do I configure this?
0
mathieu_cupryk
Asked:
mathieu_cupryk
  • 4
  • 3
  • 2
1 Solution
 
tusharashahCommented:
You can specify this property in Web.Config file as shown here:
----------------------------------------------------------------------------------------------------
 <system.web>
        <customErrors defaultRedirect="Error.aspx" mode="RemoteOnly">
            <error statusCode="404" redirect="Error404.aspx" />
        </customErrors>
</system.web>
----------------------------------------------------------------------------------------------------

defaultRedirect:   Error.aspx is the page where you'll be redirected upon any other unspecified error (Exception!)
RemoteOnly: Will show this Custom Pages at Remote only, so if you're developing locally you'll see detail error message.
You can use On/Off in place of RemoteOnly.

Error404.aspx is the page where you'll be redirected upon any Page Not found error.

-tushar
0
 
mathieu_cuprykAuthor Commented:
I mean on the server side.
0
 
tusharashahCommented:
You've to keep mode="On" to view these pages on serverside

-tushar
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
mathieu_cuprykAuthor Commented:
I mean in internet explorer sometimes the page keeps redirected to a page that you don't won't. I would like to constumize and make my own page.
0
 
tusharashahCommented:
This customize pages are called Custom Error pages. You have to specify this in your Web.Config file. For each error there's a specific code, ie. 404 for 'Page Not Found'. You need to specify this settings in Web.Config as I have shown in my first post.

Modify <customerErrors> inside your Web.config like following & create Error.aspx for general errors and Error404.aspx for Page Not found errors.

----------------------------------------------------------------------------------------------------
 <system.web>
        <customErrors defaultRedirect="Error.aspx" mode="On">
            <error statusCode="404" redirect="Error404.aspx" />
        </customErrors>
</system.web>
----------------------------------------------------------------------------------------------------

-tushar
0
 
AerosSagaCommented:
----------------------------------------------------------------------------------------------------------------
***The Global.asax.vb code
-----------------------------------------------------------------------------------------------------------------
Sub Application_Error
(ByVal sender As Object, ByVal e As EventArgs)

Try
   '----------------------------------------
   ' 1. Set default values
   '-----------------------------------------
   Session("ErrorMsg") =
   "No error information was available"
   Session("ExceptionType") = String.Empty
   Session("PageErrorOccured") = _
       Request.CurrentExecutionFilePath
   Session("StackTrace") = String.Empty
  '------------------------------------------------
  ' 2. Catch the last exception thrown
  '------------------------------------------------
  Dim lastError As Exception = Server.GetLastError()
  '-------------------------------------------------
  ' 3. Pull out the InnerException
  '------------------------------------------------------
  If (Not lastError Is Nothing) Then
      lastError = lastError.InnerException
      '-----------------------------------------------------
      ' 4. Place a few key values in the session for
      ' retrieval on the custom error page
      '------------------------------------------------------
      Session("ErrorMsg") = lastError.Message
      Session("ExceptionType") = lastError.GetType.ToString
      Session("StackTrace") = lastError.StackTrace
  End If
  '-----------------------------------------------------------
  ' 5. Clear the error from the server
  '---------------------------------------------------------------
  Server.ClearError()
  '-----------------------------------------------------------------
  ' 6. Redirect to the custom error page
  '---------------------------------------------------------------
  Response.Redirect("error.aspx")
Catch ex As Exception
   ' if we end up here,
   'error handling has thrown an error.
   ' do nothing - don't want to create an infinite loop
   Response.Write("We apologize, but an" & _
   " unrecoverable error has occurred. Please click the " & _
   " back button on your browser and try again.")
End Try
-------------------------------------------------------------------------------------------------------------------
***Your Custom Error Page
The process to follow in your error page code-behind is:
    * Add an ASP.NET label and name it lblMessage.
    * Grab the exception information placed in the session in Global.asax.vb.
    * Clear the values from the session.
    * Display a generic error message to the user. We will even include a link back to the offending page.
    * Add specific error information as HTML comments for use during development. This code should be removed when the application is moved to the production.
In the error.aspx.vb Page_Load, place the following:
-----------------------------------------------------------------------------------------------------------------
Try
   If (Not IsPostBack) Then
   '------------------------------------------------------------
   ' 1. Grab the exception information from the session
   '-----------------------------------------------------------
   Dim errorMsg As String =
   CStr(Session("ErrorMsg"))
   Dim pageErrorOccured As String =
   CStr(Session("PageErrorOccured"))
   Dim exceptionType As String =
   CStr(Session("ExceptionType"))
   Dim stackTrace As String =
   CStr(Session("StackTrace"))
   '--------------------------------------------
   ' 2. Clear the values from the session
   '---------------------------------------------
   Session("ErrorMsg") = Nothing
   Session("PageErrorOccured") = Nothing
   Session("ExceptionType") = Nothing
   Session("StackTrace") = Nothing
   '--------------------------------------------------------------
   ' 3. Display a generic error message to the user
   '-------------------------------------------------------------------------
   lblMessage.Text = "We're sorry, but an unhandled " & _
   "error has occurred.<br/><br/>"

   lblMessage.Text =
   String.Format("{0}To try again, " & _
   "click <a href='{1}' class='linkgreen'>
   here</a>.<br/><br/>", _
   lblMessage.Text, pageErrorOccured)
  '----------------------------------------------------------------------------
  ' 4. Add specific error information as
  'HTML comments for you
  ' to view during development.
  'You could also log the error to
  ' the Windows event log here.
  '----------------------------------------------------------------------------
  lblMessage.Text = lblMessage.Text & "<!--" & Chr(10) & _
  "Error Message: " & errorMsg & Chr(10) & _
  "Page Error Occurred: " & pageErrorOccured & Chr(10) & _
  "ExceptionType: " & exceptionType & Chr(10) & _
  "Stack Trace: " & stackTrace & Chr(10) & _
  "-->"
 '--------------------------------------------------------------------------
 ' 5. Send notification and debug stack info
 '-----------------------------------------------------------------------------
 Dim ErrorEmail As New Mail.MailMessage
 ErrorEmail.BodyFormat = Mail.MailFormat.Text
 ErrorEmail.From = "exceptions@ajboydjeeps.net"
 ErrorEmail.To = "sschecter@chilitech.net"
 ErrorEmail.Subject = "AJ Boyd Jeep Exception"
 ErrorEmail.Body = "An exception has occoured the debug informatin is as follows" & "<br>" & _
 errorMsg & "<br>" & _
 pageErrorOccured & "<br>" & _
 exceptionType & "<br>" & _
 stackTrace
 Mail.SmtpMail.SmtpServer = "smtp.chilitech.net"
 Mail.SmtpMail.Send(ErrorEmail)
End If
Catch ex As Exception
  ' If an exception is thrown in the
  ' above code output the message
  ' and stack trace to the screen
  lblMessage.Text = ex.Message & " " & ex.StackTrace
End Try


End Sub
-------------------------------------------------------------------------------------------------------------------
To test this code create a new web form called ThrowsAnException.aspx and modify the Page_Load as follows:
-------------------------------------------------------------------------------------------------------------------
Private Sub Page_Load(ByVal sender As System.Object, _
 ByVal e As System.EventArgs)
 Handles MyBase.Load
 Throw New Exception("Something didn't work right.")
End Sub

Upon running ThrowsAnException.aspx you will see your custom error page with the generic message and the link back to the offending page. If you view the HTML source in your browser you will see your error message ("Something didn't work right") and a stack trace in the HTML comments.

Aeros
0
 
mathieu_cuprykAuthor Commented:
I am sorry I am talking about IIS here.
I am totally confused.:-(
0
 
AerosSagaCommented:
You should really post in the IIS section, but if you go to the properties of your Virtual Directory In IIS you will find the custom errors section.

Aeros
0
 
tusharashahCommented:
Check out this article on IIS Custom Error page, that also explain how/where you should set them up: (as Aeros suggested!)
  http://www.microsoft.com/technet/prodtechnol/windows2000serv/technologies/iis/tips/custerr.mspx

-tushar
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now