質問

I am trying to do an error page which redirect to it whenever an error occurs.

This is my code:

              <customErrors defaultRedirect="Error.aspx" mode="On" />

which is working fine now how can I get the error message too on my error page

Example: Error - Index Error

役に立ちましたか?

解決

You would need to get the last error that occurred (programmatically) and display it in the page. You can do that like this (in Error.aspx):

protected void Page_Load(object sender, EventArgs e)
{
     Exception ex = Server.GetLastError();
     lblError.Text= ex.Message;
     Server.ClearError();
}

Where lblError is a Label control defined in your page just for the purpose of displaying the error messages.

See here for more details.

他のヒント

protected override void OnError(EventArgs e)
{     
  HttpContext ctx = HttpContext.Current;

  Exception exception = ctx.Server.GetLastError ();

  string errorInfo = 
     "<br>Offending URL: " + ctx.Request.Url.ToString () +
     "<br>Source: " + exception.Source + 
     "<br>Message: " + exception.Message +
     "<br>Stack trace: " + exception.StackTrace;

  ctx.Response.Write (errorInfo);
  ctx.Server.ClearError ();

  base.OnError (e);
}

Read more about ASP.NET Custom Error Pages

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top