Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NET MVC jQuery Ajax错误/异常处理_Asp.net_Asp.net Mvc 2_Error Handling - Fatal编程技术网

ASP.NET MVC jQuery Ajax错误/异常处理

ASP.NET MVC jQuery Ajax错误/异常处理,asp.net,asp.net-mvc-2,error-handling,Asp.net,Asp.net Mvc 2,Error Handling,我有一个asp.net MVC web应用程序,它使用jQuery进行大量Ajax调用。ajax调用一个控制器,该控制器捕获存储过程中抛出的各种验证异常。当在过程中发现验证异常时,我使用以下语法 RAISERROR ('VALIDATION ERROR: xyx is required.', 16, 1) 然后,我的MVC控制器捕获SQL异常,在这里我做一些日志记录,然后重新抛出一个新的异常(e.Message) 从那以后,我的ajax错误处理程序接管了任务 error: function(j

我有一个asp.net MVC web应用程序,它使用jQuery进行大量Ajax调用。ajax调用一个控制器,该控制器捕获存储过程中抛出的各种验证异常。当在过程中发现验证异常时,我使用以下语法

RAISERROR ('VALIDATION ERROR: xyx is required.', 16, 1)
然后,我的MVC控制器捕获SQL异常,在这里我做一些日志记录,然后重新抛出一个新的异常(e.Message)

从那以后,我的ajax错误处理程序接管了任务

error: function(jqXHR, textStatus, errorThrown) {}
在我的本地Visual Studio服务器中,ajax错误处理程序检索以下jqXHR.responseText所需的

<html>
<head>
    <title>VALIDATION ERROR: xyx is required.</title>
</head>
 ....
<html>
<head>
    <title>Runtime Error</title>
</head> ....

验证错误:需要xyx。
....
从那里我解析标题并显示验证错误。效果很好

但是,当我将代码部署到托管的IIS服务器时,我在jqXHR.responseText中得到一个通用的500响应:

<html>
<head>
    <title>VALIDATION ERROR: xyx is required.</title>
</head>
 ....
<html>
<head>
    <title>Runtime Error</title>
</head> ....

运行时错误
....
出于某种原因,我的共享prod服务器正在以不同的方式处理异常。您知道如何让两种环境都产生第一种行为吗

我尝试在我的web.config中添加以下行,但没有成功

<httpErrors errorMode="Custom" existingResponse="PassThrough">
  <remove statusCode="404" subStatusCode="-1" />
  <remove statusCode="502" subStatusCode="-1" />
  <remove statusCode="501" subStatusCode="-1" />
  <remove statusCode="500" subStatusCode="-1" />
</httpErrors>


提前感谢

在MVC中正确设置错误处理是一种平衡行为,至少对我来说是这样。在我的例子中,我最终在
中定义了自定义错误消息。当然,我在
Global.asax

我不会假装完全理解它,但我会告诉你我使用了什么。 我以为我的应用程序中还有一块,但我现在找不到,你可能会猜到它们的内容,它们大多是静态HTML,除了500个可能有模型的。在这种情况下,我会根据当前用户角色输出一些更详细的错误信息

我还必须解锁machine.config的
httpErrors
节点,以便应用程序能够通过web.config定义自己的自定义错误路径。您仍然可以通过IIS管理工具(相对于
web.config
)设置自己的配置,但它们实际上是通过匹配应用程序的ID写入
machine.config

web.config

<system.web>
    <customErrors mode="RemoteOnly">
      <!-- Off, RemoteOnly, On -->
      <error statusCode="400" redirect="~/errors/badrequest"/>
      <error statusCode="404" redirect="~/errors/notfound"/>
      <error statusCode="403" redirect="~/errors/forbidden"/>
      <error statusCode="500" redirect="~/errors/exception"/>
    </customErrors>
</system.web>

<system.webServer>
    <httpErrors errorMode="DetailedLocalOnly">
      <!-- Detailed, DetailedLocalOnly, Custom -->
      <remove statusCode="400" subStatusCode="-1"/>
      <error statusCode="400" path="/errors/badrequest" responseMode="ExecuteURL"/>
      <remove statusCode="403" subStatusCode="-1"/>
      <error statusCode="403" path="/errors/forbidden" responseMode="ExecuteURL"/>
      <remove statusCode="500" subStatusCode="-1"/>
      <error statusCode="500" path="/errors/exception" responseMode="ExecuteURL"/>
      <remove statusCode="404" subStatusCode="-1"/>
      <error statusCode="404" path="/errors/notfound" responseMode="ExecuteURL"/>
    </httpErrors>
</system.webServer>
错误控制器

[AllowAnonymous]
public class ErrorsController : WimsController
{
    public ActionResult Index()
    {
        return RedirectToAction("Index", "Home");
    }

    public ActionResult Exception(HandleErrorInfo error = null)
    {
        return View("Error_500", error);
    }

    public ActionResult NotFound()
    {
        return View("Error_404");
    }

    public ActionResult Forbidden()
    {
        return View("Error_403");
    }

    public ActionResult BadRequest()
    {
        return View("Error_400");
    }
}