Gerenciando Erros

<< Click to Display Table of Contents >>

Navigation:  ASP.NET >

Gerenciando Erros

Previous pageReturn to chapter overviewNext page

Maneira 1 - mais simples

 

Default.aspx

 

NADA

 

Default.aspx.cs

 

  protected void Page_Load(object sender, EventArgs e)

  {

      // isso vai causar um erro

      string s = Request.Cookies["NaoExiste"].Value;

  }

 

 

Erro.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Erro.aspx.cs" Inherits="Erro" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

  <title></title>

</head>

<body>

  <form id="form1" runat="server">

  <div>

    <h2>Deu erro</h2>

  </div>

  </form>

</body>

</html>

 

Erro.aspx.cs

 

   protected void Page_Load(object sender, EventArgs e)

   {

       Response.Write("Origem: " + Request.QueryString["aspxerrorpath"].ToString());

   }

 

web.config        

 

<?xml version="1.0"?>

<configuration>

 <system.web>

         <compilation debug="true" targetFramework="4.0"/>

         <customErrors mode="On" defaultRedirect="~/Erro.aspx">

                 <error statusCode="404" redirect="~/Erro.aspx"/>

                 <error statusCode="403" redirect="~/Erro.aspx"/>

         </customErrors>

 </system.web>

</configuration>

 


 

Maneira 2 - com mais detalhes

 

Default.aspx

 

NADA

 

Default.aspx.cs

 

  protected void Page_Load(object sender, EventArgs e)

  {

      // isso vai causar um erro

      string s = Request.Cookies["NaoExiste"].Value;

  }

 

 

Erro.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Erro.aspx.cs" Inherits="Erro" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

  <title></title>

</head>

<body>

  <form id="form1" runat="server">

  <div>

    <h2>Deu erro</h2>

  </div>

  </form>

</body>

</html>

 

Erro.aspx.cs

 

  protected void Page_Load(object sender, EventArgs e)

   {

      // Usa Server.GetLastError para receber o erro corrente

      Exception CurrentException = Server.GetLastError();

 

      // Mostra a mensagem principal

       Response.Write("<h1>Mensagem de erro: " + CurrentException.Message + "</h1>");

 

       Response.Write("<p>Fonte:</p>");

       Response.Write(CurrentException.Source);

 

       Response.Write("<p>Trace:</p>");

       Response.Write(CurrentException.StackTrace);

 

       Response.Write("<p>Inner:</p>");

       Response.Write(CurrentException.InnerException);

 

      // Detalhes

       Response.Write("<p>Detalhes:</p>");

       Response.Write(CurrentException.ToString());

   }

 

web.config

 

<?xml version="1.0"?>

<configuration>

 <system.web>

         <customErrors mode="On" defaultRedirect="~/Erro.aspx" redirectMode="ResponseRewrite"/>

         <compilation debug="true"/></system.web>

</configuration>

 


 

Maneira 3 - Usando Application_Error do Global.asax

 

Default.aspx

 

NADA

 

Default.aspx.cs

 

  protected void Page_Load(object sender, EventArgs e)

  {

      // isso vai causar um erro

      string s = Request.Cookies["NaoExiste"].Value;

  }

 

Erro.aspx

 

Não tem este arquivo!

 

web.config (aqui tá o pulo do gato)

 

NAO TA FUNCIONANDO

 

<configuration>

 <system.web>

   <compilation debug="true" targetFramework="4.0"/>

   <customErrors mode="On" defaultRedirect="~/Erro.aspx" redirectMode="ResponseRewrite" />

 </system.web>

</configuration>

 

Global.asax  (global application class)

 

  void Application_Error(object sender, EventArgs e)

   {

      // Usa Server.GetLastError para receber o erro corrente

      Exception CurrentException = Server.GetLastError();

 

      // Mostra a mensagem principal

       Response.Write("<h1>Mensagem de erro: " + CurrentException.Message + "</h1>");

 

       Response.Write("<p>Fonte:</p>");

       Response.Write(CurrentException.Source);

 

       Response.Write("<p>Trace:</p>");

       Response.Write(CurrentException.StackTrace);

 

       Response.Write("<p>Inner:</p>");

       Response.Write(CurrentException.InnerException);

 

      // Detalhes

       Response.Write("<p>Detalhes:</p>");

       Response.Write(CurrentException.ToString());

   }