无论我做什么都无法命中 OnError

发布于 2025-01-08 06:32:00 字数 1115 浏览 1 评论 0原文

已解决,见下文

在我的控制器中...

 [HttpPost]
 public ActionResult Reject(RevisionRejectModel model)
 {
    throw new HttpException(500, "Internal Error");
    ...
}

在我的视图中...

  @using (Ajax.BeginForm("Reject", "Revision", new AjaxOptions
    {
        HttpMethod = "Post",
        OnBegin = "RejectOnBegin",
        OnFailure = "RejectOnFailure",
        OnSuccess = "RejectOnSuccess",
        OnComplete = "RejectOnComplete"
    }))
    ...

在我的.js...

function RejectOnFailure(ajaxContext) {
   alert("Never hit!");
}

已解决

问题是在中打开了自定义错误网络配置... 即使在 web.config 中打开了自定义错误,也能正常工作...

我处理此问题的方法是设置 Response.StatusCode = 500 并将标头附加到我的响应对象Response.AppendHeader("MyResponseHeader", "My Message");

在我的 .js OnFailure 处理程序中...

function OnFailure(ajaxContext) {
    alert(ajaxContext.status); // 500
    alert(ajaxContext.getResponseHeader("MyResponseHeader"));  // "My Message"
}

SOLVED, see below

In my Controller...

 [HttpPost]
 public ActionResult Reject(RevisionRejectModel model)
 {
    throw new HttpException(500, "Internal Error");
    ...
}

In my View...

  @using (Ajax.BeginForm("Reject", "Revision", new AjaxOptions
    {
        HttpMethod = "Post",
        OnBegin = "RejectOnBegin",
        OnFailure = "RejectOnFailure",
        OnSuccess = "RejectOnSuccess",
        OnComplete = "RejectOnComplete"
    }))
    ...

In my .js...

function RejectOnFailure(ajaxContext) {
   alert("Never hit!");
}

SOLVED

The problem was that customerrors was turned on in web.config...
Works even with customerrors turned on in web.config...

The way I handle this, is to set the Response.StatusCode = 500 and append a Header to my response object Response.AppendHeader("MyResponseHeader", "My Message");

In my .js OnFailure handler...

function OnFailure(ajaxContext) {
    alert(ajaxContext.status); // 500
    alert(ajaxContext.getResponseHeader("MyResponseHeader"));  // "My Message"
}

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

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

发布评论

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

评论(1

如日中天 2025-01-15 06:32:00

我假设这里有一个自定义错误页面,将您重定向到错误控制器。情况可能并非如此,但首先验证 - 加载 fiddler 并在浏览器中运行对 /Revision/Reject 的请求,并查看响应代码是否为 500。

Im going to assume here you have a custom error page redirecting you to an error controller. It may not be the case, but first verify - load up fiddler and run the request in the browser to /Revision/Reject and see if the response code is 500.

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