filterContext.Result = new JsonResult()( CustomJsonExceptionHandler )无法正常工作。
请看下面的代码,函数抛出异常。自定义属性用于将错误消息返回到ajax函数
控制器功能
[HttpPost]
[CustomJsonExceptionHandler]
public ActionResult GetList(int month, int year)
{
throw new ArgumentException("Exception");
List<Dictionary<string, object>> List = new List<Dictionary<string,object>>()
return Json(List, JsonRequestBehavior.AllowGet);
}
自定义错误属性
public class CustomJsonExceptionHandler : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
if (filterContext != null && filterContext.Exception != null)
{
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.StatusCode = 500;
filterContext.Result = new JsonResult()
{
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
Data = new
{
errorMessage = filterContext.Exception.Message
}
};
}
}
}
请参阅下面的jquery ajax函数代码
$.fn.GetData = function (month, year) {
var data = null;
$.ajax({
url: '/HomeController/GetList',
data: { month: month, year: year },
type: 'POST',
async: false,
success: function (Listdata) {
data = Listdata;
},
error: function (xhr) {
var jsonerror = $.parseJSON(xhr.responseText)
alert(jsonerror.errorMessage)
}
});
return data;
}
请帮助!!
答案 0 :(得分:1)
您好问题已解决我已将以下内容添加到web.config
<system.webServer>
<httpErrors errorMode="Detailed" />
<asp scriptErrorSentToBrowser="true"/>
</system.webServer>
请参阅以下链接以供参考