【发布时间】:2015-07-13 22:11:47
【问题描述】:
我有一个 ASP.NET MVC 应用程序,我实现了一个自定义 HandleExceptionAttribute,但是当应用程序抛出一个 ajax 异常时,data.responseJSON 没有任何 JSON。
这里是 HandleExceptionAttribute
public class HandleExceptionAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
if (filterContext.HttpContext.Request.IsAjaxRequest() && filterContext.Exception != null)
{
Guid guid = Guid.NewGuid();
string guidStr = guid.ToString();
filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
filterContext.Result = new JsonResult
{
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
Data = new
{
success = false,
error = "Error al procesar su solicitud, por favor comuniquese con un administrador con el siguiente codigo: " + guidStr,
StackTrace = filterContext.Exception.StackTrace
}
};
filterContext.ExceptionHandled = true;
var properties = new Dictionary<string, string> { { "Error GUID", guidStr } };
var telemetry = new TelemetryClient();
// Send the exception telemetry:
telemetry.TrackException(filterContext.Exception, properties);
}
else
{
base.OnException(filterContext);
}
}
}
这里是使用异常来显示错误的 Javascript 函数
function AddActividad() {
...
$.ajax({
url: 'IniciativasEstrategicasWebPart/AddActividad',
type: 'POST',
data: {
idObjetivo: idObjetivoValue,
IdIniciativa: idIniciativaValue,
IniciativaName: iniciativaValue,
Fase: fase,
NombreActividad: nombreActividad,
FechaInicio: fechaInicio,
FechaVencimiento: fechaVencimiento,
Estado: estado,
Responsables: responsables
}
}).done(function (data) {
DrawFasesInActividades(data);
}).fail(function (data) {
ShowErrorMessage(data);
});
}
function ShowErrorMessage(data) {
$("#divAlertIniciativas").attr("class", "alert alert-danger");
$("#divAlertIniciativas").text(data.responseJSON.error);
$("#alertIniciativasContainer").show("slow");
location.href = "#alertIniciativasContainer"
setTimeout(
function () {
$("#alertIniciativasContainer").hide("slow");
}, 8000);
}
堆栈跟踪:
at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)
at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)
at Microsoft.SharePoint.Client.ClientRequest.ExecuteQuery()
at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQuery()
at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
at business.businessbusiness.Controllers.IniciativasEstrategicasWebPartController.AddActividad(Actividad actividad) in d:\PC\business\Developers\Dev1\business\business\Controllers\businessWebPartController.cs:line 222
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.<BeginInvokeAction>b__1c()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)
【问题讨论】:
-
你有异常的完整堆栈跟踪吗?
-
你确定这是完整的堆栈跟踪吗?它不包含异常消息,也没有提到
GetFases方法,如果我正确理解您的问题,应该抛出异常 -
对不起,这不是功能。我已经更正了 javascript 代码
-
在 localhost 中工作得很好,但在 Azure 中 data.response.JSON 为空
-
您找到解决方案了吗?
标签: javascript c# jquery asp.net asp.net-mvc