我无法得到我想要的错误处理:
从浏览器运行AJAX调用。它在控制器中触发一个Action。
如果Action中的一切正常,则应转到.done()
部分。在其他情况下,它应该继续.fail()
在我的情况下,它总是进入.done()
我希望能够在控制器中捕获异常时在.fail()
回调中执行代码。
我尝试使用HttpStatusCodeResult
或简单传播throw
,但始终通过.done()
HTML-JAVASCRIPT
function OnSelectedGraphicChanged() {
var selectedGraphic = SelectedGraphic.GetValue();
var url = '/GraphicDesigner/GenerateGraphic';
$.ajax({
type: 'GET',
url: url,
data: {
graphic: selectedGraphic
},
}).done(function (result) {
debugger;
if (!result.error) {
// do some stuff
}
ChartLoadingPanel.Hide();
}).fail(function (result) {
// handle errors
debugger;
//do some stuff
location.href = "/Home/"
});
CONTROLLER
public ActionResult GenerateGraphic(int graphic)
{
try
{
// do some stuff
return Json(new
{
error = false,
exception = false,
graphic = RenderHelper.RenderViewToString(this.ControllerContext, "Graphic/_displayChart", model),
}, JsonRequestBehavior.AllowGet);
}
catch (Exception)
{
return new HttpStatusCodeResult(500, "Unexpected error");
//throw;
}
}
的global.asax.cs
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = System.Web.HttpContext.Current.Server.GetLastError();
//TODO: Handle Exception
Session.Clear();
Server.ClearError();
}