首页 文章

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

提问于
浏览
0

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

RAISERROR ('VALIDATION ERROR: xyx is required.', 16, 1)

然后我的MVC Controller捕获SQL异常,我在那里进行一些日志记录,然后重新抛出一个新的异常(e.Message) .

catch (SqlException se)
{
    // logging happens

    // exception get rethrown for ajax error handling
    throw new Exception(se.Message);
 }

从那里,我的ajax错误处理程序接管 .

error: function(jqXHR, textStatus, errorThrown) {}

在我的本地Visual Studio Server中,ajax错误处理程序在jqXHR.responseText之后检索 desired

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

从那里我解析 Headers 并显示验证错误 . 效果很好 .

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

<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>

提前致谢

1 回答

  • 0

    在MVC中正确设置错误处理是对 balancer 行为的分类,至少对我来说是这样 . 在我的情况下,我最终在 <system.web><customErrors><system.webServer><httpErrors> 中定义了自定义错误消息 . 然后当然我在 Global.asax 中有默认的 ErrorHandler (或者在我的情况下是 ElmahErrorHandler

    我不会假装完全理解它,但我会告诉你我使用的是什么 . 我以为我的应用程序中还有一个部分,但我目前找不到它,你可能猜到它们的内容,它们大多是静态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>
    

    global.asax

    filters.Add(new Elmah.Contrib.Mvc.ElmahHandleErrorAttribute());
    // -- OR --
    filters.Add(new HandleErrorAttribute());
    

    Error Controller

    [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");
        }
    }
    

相关问题