How to avoid runtime errors in asp.net application

I have an asp.net application. When I opened the application, I received a run-time exception with a description saying: "An unhandled exception occurred during the execution of the current web request. Look at the stack trace for more information about the error and where it occurred in the code."

When I get this exception at runtime, I want to redirect the page to another aspx page. I added a custom error tag to the web.config file as:

<customErrors
   mode="RemoteOnly" 
   defaultRedirect="~/Error.aspx" />

But still, you will get a runtime error page instead of an error page.

+3
source share
4 answers

, : On, Off RemoteOnly.

  • RemoteOnly , , ; ASP.NET. (IE, , ), Error.aspx. .
  • . , , , Error.aspx. On .
  • . , , ASP.NET ( ). . , , .:)

Error.aspx, . Error.aspx , - ASP.NET .

+4

, customErrors , Off RemoteOnly. , -, , , .

0

RemoteOnly , ASP.NET ( msdn).
? , , customErrors = "off".

0

Application_Error Global.asax, . 404 . 404 404?

, . Server.GetLastError():

, , HttpException - . , HttpException, , Http, , . GetHttpCode(), Http 404.

void Application_Error (object sender, EventArgs e)

{

Exception ex = Server.GetLastError();

if (ex is HttpException)

{

    if (((HttpException)(ex)).GetHttpCode() == 404)

        Server.Transfer("~/Error404.aspx");

}

// Code that runs when an unhandled error occurs

Server.Transfer("~/DefaultError.aspx");

}

0
source

Source: https://habr.com/ru/post/1717522/


All Articles