0

我有以下自定义过滤器,从这个答案借来的:

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 错误,或者我能做什么?

4

1 回答 1

0

你已经[HttpGet]采取了行动,但后来你正在使用这个:

var res = (JsonNetResult)Json(model.Items, null, null, JsonRequestBehavior.DenyGet);

尝试删除:

JsonRequestBehavior.DenyGet
于 2015-11-01T05:59:01.750 回答