我在本地从.net MVC应用程序取回请求的数据时遇到零问题,但是在服务器上,ajax jquery调用报告了500错误。通常,我可以在本地遍历代码,并找出导致服务器上出现500错误的“可能”。
这次我意识到我真的需要一个更好的编码策略来捕获错误并记录错误或将错误报告给我
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做到这一点。
这里要注意的另一点是,默认情况下,在Web api中,您不会将所有错误都视为described here。为了确保捕获所有异常,请添加以下代码,并在启动时通过添加config.Filters.Add(new RestfulModelStateFilterAttribute())在方法注册的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));
}
}
关于jquery - 仅在服务器上发生500个错误之谜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33769778/