gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:58:43 24 4
gpt4 key购买 nike

我有一个asp.net MVC Web应用程序,它使用jQuery进行了许多Ajax调用。 Ajax调用一个Controller,该Controller捕获存储过程中引发的各种Validation Exception。在过程中发现验证异常时,我使用以下语法

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之后检索所需的 :
<html>
<head>
<title>VALIDATION ERROR: xyx is required.</title>
</head>
....

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

但是,当我将代码部署到托管的IIS服务器时,在jqXHR.responseText中得到了通用500响应:
<html>
<head>
<title>Runtime Error</title>
</head> ....

由于某些原因,我的共享产品服务器对异常的处理方式有所不同。您知道我如何让两种环境都产生第一行为吗?

我尝试在我的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中正确设置错误处理是一种平衡行为,至少对我而言是如此。就我而言,我最终在<system.web><customErrors><system.webServer><httpErrors>中定义了自定义错误消息。然后,当然,我在ErrorHandler中具有默认的ElmahErrorHandler(或者在我的情况下为Global.asax)

我不会假装完全理解它,但是我将向您展示我的用法。
我以为我的应用程序中还有一块,但目前找不到,您可能会猜到的 View 的内容,它们大多是静态HTML,除了500个可能有模型的HTML之外。在那种情况下,我将根据当前用户角色输出一些更详细的错误信息。

我还必须解锁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());

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

关于asp.net - ASP.NET MVC jQuery Ajax错误/异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14822089/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com