Jquery 500错误仅在服务器上发生
从.NETMVC应用程序本地获取请求的数据没有问题,但是在服务器上,ajax jquery调用报告了一个500错误。通常,我可以在本地逐步检查代码,找出服务器上500错误的“可能”原因 这一次,我意识到我真的需要一个更好的编码策略来捕获错误并记录它们或将它们报告给我Jquery 500错误仅在服务器上发生,jquery,asp.net-mvc,asp.net-web-api,error-handling,Jquery,Asp.net Mvc,Asp.net Web Api,Error Handling,从.NETMVC应用程序本地获取请求的数据没有问题,但是在服务器上,ajax jquery调用报告了一个500错误。通常,我可以在本地逐步检查代码,找出服务器上500错误的“可能”原因 这一次,我意识到我真的需要一个更好的编码策略来捕获错误并记录它们或将它们报告给我 public ActionResult PhotoList(int tblGateCode_ID) { try { context = new DBGate(); var images
public ActionResult PhotoList(int tblGateCode_ID)
{
try
{
context = new DBGate();
var images = context.tblGateCodeImages.Where(x => x.tblGateCode_ID == tblGateCode_ID);
foreach (var img in images)
{
img.GatePhoto = null;
}
ViewModel viewModel = new ViewModel
{
GateCodeImageList = images.ToList()
};
return Json(viewModel, "application/json", JsonRequestBehavior.AllowGet);
}
catch (Exception e)
{
//??
}
}
}
更新:
我确实试着在我的捕获中添加代码
catch (Exception e)
{
return Json(e.Message, "application/json", JsonRequestBehavior.AllowGet);
}
要么我不被允许这么做。。。还是因为其他原因我还拿到500
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
这有多种原因,从缺少依赖项到服务器设置不正确。因此,我建议设置一些适当的错误日志,您很快就会从这些日志中看到错误是什么 在ASP.net中执行此操作的更简单方法是安装Elmah。通过nuget实现这一点 这里需要注意的另一点是,默认情况下,在WebAPI中,您不会将所有错误作为错误来处理。为了确保捕获所有异常,请添加以下代码,并在启动时通过添加config.Filters.add(new RestfulModelStateFilterAttribute())将其注册到方法register中的WebAppConfig类中 下面的替代方法是添加nuget包Elmah.Contrib.WebApi
using System;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using Elmah;
using Newtonsoft.Json;
/// <summary>
/// see http://sergeyakopov.com/2015/03/restful-validation-with-aspnet-web-api-and-fluentvalidation
/// and https://github.com/rdingwall/elmah-contrib-webapi/blob/master/src/Elmah.Contrib.WebApi/ElmahHandleErrorApiAttribute.cs
/// this is a combination of both, but with logging enabled for anything above 400, not 500 like the link above.
/// </summary>
public class RestfulModelStateFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
base.OnActionExecuted(actionExecutedContext);
var e = actionExecutedContext.Exception;
if (e != null)
{
RaiseOrLog(e, actionExecutedContext);
}
else if ((int)actionExecutedContext.Response.StatusCode >= 400)
{
RaiseOrLog(
new HttpException(
(int)actionExecutedContext.Response.StatusCode,
ResolveMessage(actionExecutedContext)),
actionExecutedContext);
}
}
private string ResolveMessage(HttpActionExecutedContext actionExecutedContext)
{
const string messageKey = "Message";
var defaultMessage = actionExecutedContext.Response.ReasonPhrase;
var objectContent = actionExecutedContext.Response.Content as ObjectContent<HttpError>;
if (objectContent == null) return defaultMessage;
var value = objectContent.Value as HttpError;
if (value == null) return defaultMessage;
if (!value.ContainsKey(messageKey)) return defaultMessage;
var message = value[messageKey] as string;
return string.IsNullOrWhiteSpace(message) ? defaultMessage : message;
}
private void RaiseOrLog(Exception exception, HttpActionExecutedContext actionExecutedContext)
{
if (RaiseErrorSignal(exception) // prefer signaling, if possible
|| IsFiltered(actionExecutedContext)) // filtered?
return;
LogException(exception);
}
private static bool RaiseErrorSignal(Exception e)
{
var context = HttpContext.Current;
if (context == null)
return false;
var application = HttpContext.Current.ApplicationInstance;
if (application == null)
return false;
var signal = ErrorSignal.Get(application);
if (signal == null)
return false;
signal.Raise(e, context);
return true;
}
private static bool IsFiltered(HttpActionExecutedContext context)
{
var config = HttpContext.Current.GetSection("elmah/errorFilter")
as ErrorFilterConfiguration;
if (config == null)
return false;
var testContext = new ErrorFilterModule.AssertionHelperContext(
context.Exception, HttpContext.Current);
return config.Assertion.Test(testContext);
}
private static void LogException(Exception e)
{
var context = HttpContext.Current;
Elmah.ErrorLog.GetDefault(context).Log(new Error(e, context));
}
}
使用系统;
Net系统;
使用System.Net.Http;
使用System.Web;
使用System.Web.Http;
使用System.Web.Http.Controller;
使用System.Web.Http.Filters;
使用Elmah;
使用Newtonsoft.Json;
///
///看http://sergeyakopov.com/2015/03/restful-validation-with-aspnet-web-api-and-fluentvalidation
///及https://github.com/rdingwall/elmah-contrib-webapi/blob/master/src/Elmah.Contrib.WebApi/ElmahHandleErrorApiAttribute.cs
///这是两者的结合,但是对于400以上的任何对象都启用了日志记录,而不是像上面的链接那样启用500。
///
公共类RestfulModelStateFilterAttribute:ActionFilterAttribute
{
公共覆盖无效OnActionExecuted(HttpActionExecuteContext ActionExecuteContext)
{
base.OnActionExecuted(actionExecutedContext);
var e=ActionExecuteContext.Exception;
如果(e!=null)
{
RaiseOrLog(e,actionExecutedContext);
}
如果((int)ActionExecuteContext.Response.StatusCode>=400),则为else
{
RaiseOrLog(
新的HttpException(
(int)actionExecutedContext.Response.StatusCode,
ResolveMessage(actionExecutedContext)),
actionExecutedContext);
}
}
私有字符串解析消息(HttpActionExecuteContext ActionExecuteContext)
{
常量字符串messageKey=“Message”;
var defaultMessage=actionExecutedContext.Response.reasonPhase;
var objectContent=actionExecutedContext.Response.Content作为objectContent;
if(objectContent==null)返回defaultMessage;
var value=objectContent.value作为HttpError;
if(value==null)返回defaultMessage;
如果(!value.ContainsKey(messageKey))返回defaultMessage;
var message=字符串形式的值[messageKey];
返回字符串.IsNullOrWhiteSpace(message)?defaultMessage:message;
}
私有void RaiseOrLog(异常,HttpActionExecuteContext ActionExecuteContext)
{
if(RaiseErrorSignal(exception)//如果可能,首选信令
||IsFiltered(ActionExecuteContext))//是否已筛选?
返回;
日志异常(异常);
}
专用静态bool RaiseErrorSignal(异常e)
{
var context=HttpContext.Current;
if(上下文==null)
返回false;
var application=HttpContext.Current.ApplicationInstance;
if(应用程序==null)
返回false;
var信号=ErrorSignal.Get(应用程序);
如果(信号==null)
返回false;
信号。升起(e,上下文);
返回true;
}
私有静态bool已筛选(HttpActionExecutedContext)
{
var config=HttpContext.Current.GetSection(“elmah/errorFilter”)
as错误过滤器配置;
if(config==null)
返回false;
var testContext=new ErrorFilterModule.AssertionHelperContext(
异常,HttpContext.Current);
返回config.Assertion.Test(testContext);
}
私有静态无效日志异常(异常e)
{
var context=HttpContext.Current;
Elmah.ErrorLog.GetDefault(context.Log(新错误(e,context));
}
}
这有多种原因,从缺少依赖项到服务器设置不正确都有。因此,我建议设置一些适当的错误日志,您很快就会从这些日志中看到错误是什么
在ASP.net中执行此操作的更简单方法是安装Elmah。通过nuget实现这一点
这里需要注意的另一点是,默认情况下,在WebAPI中,您不会将所有错误作为错误来处理。为了确保捕获所有异常,请添加以下代码,并在启动时通过添加config.Filters.add(new RestfulModelStateFilterAttribute())将其注册到方法register中的WebAppConfig类中
下面的替代方法是添加nuget包Elmah.Contrib.WebApi
using System;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using Elmah;
using Newtonsoft.Json;
/// <summary>
/// see http://sergeyakopov.com/2015/03/restful-validation-with-aspnet-web-api-and-fluentvalidation
/// and https://github.com/rdingwall/elmah-contrib-webapi/blob/master/src/Elmah.Contrib.WebApi/ElmahHandleErrorApiAttribute.cs
/// this is a combination of both, but with logging enabled for anything above 400, not 500 like the link above.
/// </summary>
public class RestfulModelStateFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
base.OnActionExecuted(actionExecutedContext);
var e = actionExecutedContext.Exception;
if (e != null)
{
RaiseOrLog(e, actionExecutedContext);
}
else if ((int)actionExecutedContext.Response.StatusCode >= 400)
{
RaiseOrLog(
new HttpException(
(int)actionExecutedContext.Response.StatusCode,
ResolveMessage(actionExecutedContext)),
actionExecutedContext);
}
}
private string ResolveMessage(HttpActionExecutedContext actionExecutedContext)
{
const string messageKey = "Message";
var defaultMessage = actionExecutedContext.Response.ReasonPhrase;
var objectContent = actionExecutedContext.Response.Content as ObjectContent<HttpError>;
if (objectContent == null) return defaultMessage;
var value = objectContent.Value as HttpError;
if (value == null) return defaultMessage;
if (!value.ContainsKey(messageKey)) return defaultMessage;
var message = value[messageKey] as string;
return string.IsNullOrWhiteSpace(message) ? defaultMessage : message;
}
private void RaiseOrLog(Exception exception, HttpActionExecutedContext actionExecutedContext)
{
if (RaiseErrorSignal(exception) // prefer signaling, if possible
|| IsFiltered(actionExecutedContext)) // filtered?
return;
LogException(exception);
}
private static bool RaiseErrorSignal(Exception e)
{
var context = HttpContext.Current;
if (context == null)
return false;
var application = HttpContext.Current.ApplicationInstance;
if (application == null)
return false;
var signal = ErrorSignal.Get(application);
if (signal == null)
return false;
signal.Raise(e, context);
return true;
}
private static bool IsFiltered(HttpActionExecutedContext context)
{
var config = HttpContext.Current.GetSection("elmah/errorFilter")
as ErrorFilterConfiguration;
if (config == null)
return false;
var testContext = new ErrorFilterModule.AssertionHelperContext(
context.Exception, HttpContext.Current);
return config.Assertion.Test(testContext);
}
private static void LogException(Exception e)
{
var context = HttpContext.Current;
Elmah.ErrorLog.GetDefault(context).Log(new Error(e, context));
}
}
使用系统;
Net系统;
使用System.Net.Http;
使用System.Web;
使用System.Web.Http;
使用System.Web.Http.Controller;
使用System.Web.Http.Filters;
使用Elmah;
使用Newtonsoft.Json;
///
///看http://sergeyakopov.com/2015/03/restful-validation-with-aspnet-web-api-and-fluentvalidation
///及https://github.com/rdingwall/elmah-contrib-webapi/blob/master/src/Elmah.Contrib.WebApi/ElmahHandleErrorApiAttribute.cs
///这是两者的结合,但是对于400以上的任何对象都启用了日志记录,而不是像上面的链接那样启用500。
///
公共类RestfulModelStateFilterAttribute:ActionFilterAttribute
{
公共覆盖无效OnActionExecuted(HttpAction