How to handle ASP.NET application error that occurs on application start and transfer & display error in MVC view?

前端 未结 1 981
南方客
南方客 2020-12-19 00:03

I know that ASP.NET MVC has error filter attribute to handle specified error type. However, this feature cannot catch any error that occurs when application start. Therefore

相关标签:
1条回答
  • 2020-12-19 00:45

    The reason why Request and Response objects are null inside your Application_Error event is because the Application_Start event is not associated with any user, therefore you cannot redirect from it. Only the Application and Server built-in objects are available. Referencing the Session, Request, or Response objects would cause an error.

    One workaround is to use try/catch and in the catch block log the error and set some global variable which indicates that something went wrong during application startup. Later on Application_BeginRequest you could check this variable and redirect accordingly.

    0 讨论(0)
提交回复
热议问题