服务器 2008 上的 global.asax MVC 3.0 问题中的错误处理

发布于 2024-11-29 13:07:29 字数 3303 浏览 5 评论 0原文

我创建后

protected void Application_Error(object sender, EventArgs e)
{
   if (Context.Handler is IRequiresSessionState || Context.Handler is IReadOnlySessionState)
   {
      try
      {
         var httpContext = HttpContext.Current;
         Exception exception = httpContext.Server.GetLastError();

         if (exception is HttpRequestValidationException)
         {
            httpContext.Response.Clear();
            var logLoggerService = new LogLoggerService();
            logLoggerService.Error("ValidationException", exception);
            WriteErrorResponse(exception.Message);
         }
         else
         {
            httpContext.Response.Clear();
            var logLoggerService = new LogLoggerService();

            logLoggerService.Error(exception);
            WriteErrorResponse(exception.Message);
         }
     }
     catch
     {
        var httpContext = HttpContext.Current;
        httpContext.Response.Clear();
     }
  }
}

发现,当服务器发生错误时,不会调用此函数。但是当我处于调试模式时,会调用 Application_Error

为什么这在服务器上不起作用?也许服务器上的 IIS 或 .NET 4.0 有问题?

在服务器上我有:

事件代码:3005 事件消息:发生了未处理的异常。 活动时间:2011年8月15日8:25:26 活动时间(UTC):2011年8月15日6:25:26 事件 ID:ae3cad590adc43c3803410023cccb6b0 事件顺序:4 事件发生:1 活动详细代码:0

申请信息: 应用域:/LM/W3SVC/11/ROOT-1-129578631124796958 信任级别:完全 应用程序虚拟路径:/ 应用程序路径:C:\inetpub\wwwroot\TestnaAplikacijaZaLog\ 机器名称:SRJ1-SRV-01

工艺信息: 进程ID:6644 进程名称: w3wp.exe 帐户名:IIS APPPOOL\TestnaAplikacijaZaLog

异常信息: 异常类型:HttpException 异常消息:在此上下文中响应不可用。 在 System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext 上下文,HttpApplication 应用程序) 在 System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext,HttpContext 上下文,MethodInfo[] 处理程序) 在 System.Web.HttpApplication.InitSpecial(HttpApplicationState 状态、MethodInfo[] 处理程序、IntPtr appContext、HttpContext 上下文) 在 System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext,HttpContext 上下文) 在 System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext)

响应在此上下文中不可用。 在 System.Web.HttpContext.get_Response() 在 c:\users\esmira\documents\visual studio 2010\Projects\LogiranjeGresaka\LogiranjeGresaka\Global.asax.cs 中的 LogiranjeGresaka.MvcApplication.Application_Start():第 46 行

请求信息: 请求网址:http://192.168.0.100:2227/Home/Index 请求路径:/Home/Index 用户主机地址:192.168.0.103 用户:
是否已验证:假 身份验证类型:
线程帐户名称:IIS APPPOOL\TestnaAplikacijaZaLog

线程信息: 线程 ID:7 线程帐户名称:IIS APPPOOL\TestnaAplikacijaZaLog 是否冒充:假 堆栈跟踪:位于 System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) 在 System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext,HttpContext 上下文,MethodInfo[] 处理程序) 在 System.Web.HttpApplication.InitSpecial(HttpApplicationState 状态,MethodInfo[] 处理程序,IntPtr appContext,HttpContext 上下文) 在 System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext,HttpContext 上下文) 在 System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext)

自定义事件详细信息:

I created

protected void Application_Error(object sender, EventArgs e)
{
   if (Context.Handler is IRequiresSessionState || Context.Handler is IReadOnlySessionState)
   {
      try
      {
         var httpContext = HttpContext.Current;
         Exception exception = httpContext.Server.GetLastError();

         if (exception is HttpRequestValidationException)
         {
            httpContext.Response.Clear();
            var logLoggerService = new LogLoggerService();
            logLoggerService.Error("ValidationException", exception);
            WriteErrorResponse(exception.Message);
         }
         else
         {
            httpContext.Response.Clear();
            var logLoggerService = new LogLoggerService();

            logLoggerService.Error(exception);
            WriteErrorResponse(exception.Message);
         }
     }
     catch
     {
        var httpContext = HttpContext.Current;
        httpContext.Response.Clear();
     }
  }
}

I found that when error occurred on server, this function is not call. But when I am in debug mode, the Application_Error is called.

Why this does not work on server? Maybe problem with IIS or with .NET 4.0 on server?

On server I have:

Event code: 3005
Event message: An unhandled exception has occurred.
Event time: 15-8-2011 8:25:26
Event time (UTC): 15-8-2011 6:25:26
Event ID: ae3cad590adc43c3803410023cccb6b0
Event sequence: 4
Event occurrence: 1
Event detail code: 0

Application information:
Application domain: /LM/W3SVC/11/ROOT-1-129578631124796958
Trust level: Full
Application Virtual Path: /
Application Path: C:\inetpub\wwwroot\TestnaAplikacijaZaLog\
Machine name: SRJ1-SRV-01

Process information:
Process ID: 6644
Process name: w3wp.exe
Account name: IIS APPPOOL\TestnaAplikacijaZaLog

Exception information:
Exception type: HttpException
Exception message: Response is not available in this context.
at System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app)
at System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers)
at System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context)
at System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context)
at System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext)

Response is not available in this context.
at System.Web.HttpContext.get_Response()
at LogiranjeGresaka.MvcApplication.Application_Start() in c:\users\esmira\documents\visual studio 2010\Projects\LogiranjeGresaka\LogiranjeGresaka\Global.asax.cs:line 46

Request information:
Request URL: http://192.168.0.100:2227/Home/Index
Request path: /Home/Index
User host address: 192.168.0.103
User:
Is authenticated: False
Authentication Type:
Thread account name: IIS APPPOOL\TestnaAplikacijaZaLog

Thread information:
Thread ID: 7
Thread account name: IIS APPPOOL\TestnaAplikacijaZaLog
Is impersonating: False
Stack trace: at System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app)
at System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers)
at System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context)
at System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context)
at System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext)

Custom event details:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

摇划花蜜的午后 2024-12-06 13:07:29

我通过添加解决了问题

<system.web>
  <customErrors mode="Off" />
</system.web>

I solve problem with adding

<system.web>
  <customErrors mode="Off" />
</system.web>
情独悲 2024-12-06 13:07:29

正在调用此方法,但上面的代码引发了错误,如错误消息所示:

<块引用>

异常信息:异常类型:HttpException
异常消息:在此上下文中响应不可用

您正在尝试使用响应,但不能。

This is being called, but the error is being thrown by the code above as detailed by the error message:

Exception information: Exception type: HttpException
Exception message: Response is not available in this context

You are trying to use Response, but you can't.

暮色兮凉城 2024-12-06 13:07:29

就在这里,它告诉您问题所在的行号:

在此上下文中响应不可用。在 c:\users\esmira\documents\visual studio 2010\Projects\LogiranjeGresaka\LogiranjeGresaka\Global.asax.cs 中 LogiranjeGresaka.MvcApplication.Application_Start() 处的 System.Web.HttpContext.get_Response() 处:第 46 行< /strong>

第 46 行
全局.asax

Right here, it is telling you the line number where the problem is:

Response is not available in this context. at System.Web.HttpContext.get_Response() at LogiranjeGresaka.MvcApplication.Application_Start() in c:\users\esmira\documents\visual studio 2010\Projects\LogiranjeGresaka\LogiranjeGresaka\Global.asax.cs:line 46

Line 46
Global.asax

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文