5

我有以下模型:

public class Resource
{
    [DataMember(IsRequired = true)]
    [Required]
    public bool IsPublic { get; set; }

    [DataMember(IsRequired = true)]
    [Required]
    public ResourceKey ResourceKey { get; set; }
}

public class ResourceKey
{
    [StringLength(50, MinimumLength = 1)]
    [Required]
    public string SystemId { get; set; }

    [StringLength(50, MinimumLength = 1)]
    [Required]
    public string SystemDataIdType { get; set; }

    [StringLength(50, MinimumLength = 1)]
    [Required]
    public string SystemEntityType { get; set; }

    [StringLength(50, MinimumLength = 1)]
    [Required]
    public string SystemDataId { get; set; }
}

我有以下动作方法签名:

public HttpResponseMessage PostResource(Resource resource)

我在正文中发送以下带有 JSON 的请求(属性“IsPublic”的故意无效值):

Request Method:POST
Host: localhost:63307
Connection: keep-alive
Content-Length: 477
User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.97 Safari/537.22
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

{
    "IsPublic": invalidvalue,   
    "ResourceKey":{     
        "SystemId": "asdf",
        "SystemDataIdType": "int",
        "SystemDataId": "Lorem ipsum",
        "SystemEntityType":"EntityType"
    },    
}

这是无效的 JSON - 通过 JSONLint 运行它,它会告诉您:

第 2 行的解析错误:

{“IsPublic”:无效值,

..................^ 期待 'STRING'、'NUMBER'、'NULL'、'TRUE'、'FALSE'、'{'、'['

ModelState.IsValid 属性为“真” - 为什么???

此外,格式化程序似乎放弃了反序列化并简单地将“资源”参数作为空值传递给操作方法,而不是引发验证错误!

请注意,如果我为其他属性输入无效值,也会发生这种情况,例如替换:

"SystemId": notAnObjectOrLiteralOrArray

但是,如果我为“SystemId”属性发送带有特殊未定义值的以下 JSON:

{
    "IsPublic": true,   
    ResourceKey:{       
        "SystemId": undefined,
        "SystemDataIdType": "int",
        "SystemDataId": "Lorem ipsum",
        "SystemEntityType":"EntityType"
    },    
}

然后我得到以下合理的异常抛出:

Exception Type: Newtonsoft.Json.JsonReaderException
Message: "Error reading string. Unexpected token: Undefined. Path 'ResourceKey.SystemId', line 4, position 24."
Stack Trace: " at Newtonsoft.Json.JsonReader.ReadAsStringInternal() 
at Newtonsoft.Json.JsonTextReader.ReadAsString() 
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter) 
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)"

SO:Newtonsoft.Json 库中发生了什么导致部分 JSON 验证的结果???

PS:可以将 JSON 名称/值对发布到 Web API 而不用引号将名称括起来......

{
    IsPublic: true, 
    ResourceKey:{       
        SystemId: "123",
        SystemDataIdType: "int",
        SystemDataId: "Lorem ipsum",
        SystemEntityType:"EntityType"
    },    
}

这也是无效的 JSON!

4

2 回答 2

3

好的 - 所以看起来部分问题是由我自己的行为引起的。

我在控制器上有两个过滤器:

  1. 检查是否有任何 null 操作参数传递给操作方法,如果是,则返回“400 Bad Request”响应,规定参数不能为 null。
  2. 一个 ModelState 检查过滤器,它检查 ModelState 的错误,如果发现任何错误,则在“400 Bad Request”响应中返回它们。

我犯的错误是将空参数过滤器放在模型状态检查过滤器之前。

在模型绑定之后,第一个 JSON 示例的序列化将正确失败,并将相关的序列化异常放入 ModelState 中,并且 action 参数将保持为空,这是正确的。

但是,由于第一个过滤器正在检查空参数,然后返回“404 Bad Request”响应,因此 ModelState 过滤器从未启动...

因此,似乎没有进行验证,而实际上确实发生了,但结果被忽略了!

重要提示:在模型绑定期间发生的序列化异常被放置在 ModelState KeyValue 对值的“异常”属性中...而不是在 ErrorMessage 属性中!

为了帮助其他人区分这种情况,这是我的 ModelValidationFilterAttribute:

public class ModelValidationFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        if (actionContext.ModelState.IsValid) return;

        // Return the validation errors in the response body.
        var errors = new Dictionary<string, IEnumerable<string>>();
        foreach (KeyValuePair<string, ModelState> keyValue in actionContext.ModelState)
        {
            var modelErrors = keyValue.Value.Errors.Where(e => e.ErrorMessage != string.Empty).Select(e => e.ErrorMessage).ToList();
            if (modelErrors.Count > 0)
                errors[keyValue.Key] = modelErrors;

            // Add details of any Serialization exceptions as well
            var modelExceptions = keyValue.Value.Errors.Where(e => e.Exception != null).Select(e => e.Exception.Message).ToList();
            if (modelExceptions.Count > 0)
                errors[keyValue.Key + "_exception"] = modelExceptions;
        }
        actionContext.Response =
            actionContext.Request.CreateResponse(HttpStatusCode.BadRequest, errors);
    }
}

这是操作方法,过滤器的顺序正确:

    [ModelValidationFilter]
    [ActionArgNotNullFilter]
    public HttpResponseMessage PostResource(Resource resource)

所以现在,下面的 JSON 结果是:

{
    "IsPublic": invalidvalue,   
    "ResourceKey":{     
        "SystemId": "asdf",
        "SystemDataIdType": "int",
        "SystemDataId": "Lorem ipsum",
        "SystemEntityType":"EntityType"
    },    
} 

{
    "resource.IsPublic_exception": [(2)
    "Unexpected character encountered while parsing value: i. Path 'IsPublic', line 2, position 21.",
    "Unexpected character encountered while parsing value: i. Path 'IsPublic', line 2, position 21."
    ]-
}

然而,所有这些都不能解释为什么无效的 JSON 仍然被 JsonMediaTypeFormatter 解析,例如它不需要名称是字符串。

于 2013-02-27T10:58:19.643 回答
1

解决方法比答案更多,但我能够使用http://aspnetwebstack.codeplex.com/workitem/609上发布的解决方法来解决这个问题。基本上,与其让 Post 方法的签名采用 Resource 实例,不如让它不采用任何参数,然后使用 JSon.Net(或 JsonMediaTypeFormatter 的新实例)进行反序列化。

public void Post()
{
    var json = Request.Content.ReadAsStringAsync().Result;
    var resource = Newtonsoft.Json.JsonConvert.DeserializeObject<Resource>(json);

    //Important world saving work going on here
}
于 2013-02-26T21:58:23.350 回答