1

考虑这段代码。

        protected void Application_Start()
        {
            InitLogger();

            InitAppContext();

            AreaRegistration.RegisterAllAreas();

            // WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();            
        }

假设 InitLogger 发生异常。

我看到了两种解决方案来处理这个问题:

  1. 使用 Application_Error 事件将请求重定向到某个页面。如果我尝试这种方法,我会得到Request is not available in this context
  2. 使用<customErrors mode="On" defaultRedirect="Error.html" />. 如果我尝试他的,我会得到http://localhost:1937/Error.html?aspxerrorpath=/

如何处理描述的情况?

谢谢。

PS:为了处理控制器异常(http异常和自定义异常),我使用了一个过滤器,这部分已经完成。

4

1 回答 1

2

Both your solutions talk about redirecting the request to somewhere else. However, your exception is occurring at application startup, and as such there hasn't been a request yet.

If InitLogger() can throw an exception, wrap the call in a try/catch block. If you can handle the exception in some way (for example, perhaps you have a second logger that you can try instead, or your application can run without a logger), then go ahead and handle it. If you can't, well - your application isn't going to work, and since you can't fix it on-the-fly you need to record what went wrong somewhere (to help you investigate later). For example, you could write the error to the Event Log, although note that this will happen automatically anyway if you don't catch the exception.

于 2013-06-21T13:30:50.593 回答