我有以下自定义过滤器,来自this answer:
public class JsonErrorAttribute: FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException(nameof(filterContext));
}
if (filterContext.Exception != null)
{
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
var code = filterContext.HttpContext.Response.StatusCode;
filterContext.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
filterContext.Result = new JsonResult() { Data = filterContext.Exception.Message };
}
}
}
然而,当我尝试发布标有[HttpGet]
的动作时,例如
[HttpGet]
[JsonError]
public JsonNetResult IndexData()
{
var bundles = Db.PointsBundles;
var model = new PointsBundleBundleIndexViewModel();
model.MapItems(bundles);
var res = (JsonNetResult)Json(model.Items, null, null, JsonRequestBehavior.DenyGet);
return res;
}
我收到标准的HTML 404错误响应。我怀疑,当然,这是因为404检测本身并不是一个例外,并没有被我的处理程序捕获。是否有我可以派生的另一个过滤器来返回Json响应404错误,或者我该怎么办?
答案 0 :(得分:0)
您已将[HttpGet]
放在您的操作上,但稍后您正在使用此操作:
var res = (JsonNetResult)Json(model.Items, null, null, JsonRequestBehavior.DenyGet);
尝试删除:
JsonRequestBehavior.DenyGet