假設禁止RequiredAttribute標籤驗證我發送者的要求,我的API:爲JsonMediaTypeFormatter中的ASP.NET Web API
POST http://localhost:4940/api/cars HTTP/1.1
User-Agent: Fiddler
Host: localhost:4940
Content-Type: application/json
Content-Length: 44
{"Make":"Make1","Year":2010,"Price":10732.2}
和我有以下汽車類定義:
響應我回來瞭如下:
HTTP/1.1 400 Bad Request
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RTpcRHJvcGJveFxCb29rc1xQcm9XZWJBUEkuU2FtcGxlc1xDaGFwdGVyMTNcRGF0YUFubm90YXRpb25WYWxpZGF0aW9uQXR0cmlidXRlc1NhbXBsZVxEYXRhQW5ub3RhdGlvblZhbGlkYXRpb25BdHRyaWJ1dGVzU2FtcGxlXGFwaVxjYXJz?=
X-Powered-By: ASP.NET
Date: Mon, 17 Sep 2012 11:38:58 GMT
Content-Length: 182
{"Message":"The request is invalid.","ModelState":{"car":["Required property 'Model' not found in JSON. Path '', line 1, position 44."],"car.Model":["The Model field is required."]}}
這裏是更可讀的消息體的形式:
{
"Message": "The request is invalid.",
"ModelState": {
"car": [
"Required property 'Model' not found in JSON. Path '', line 1, position 44."
],
"car.Model":[
"The Model field is required."
]
}
}
正如你所看到的,我也有車一個額外的錯誤信息,我猜JsonMediaTypeFormatter
進行驗證,以及和它未能執行讀操作。
這是問題嗎?有沒有什麼辦法可以抑制JsonMediaTypeFormatter級別的驗證?我不希望格式化程序執行驗證,因爲在格式化程序讀取消息正文後,也會通過IBodyModelValidator
執行驗證。
編輯:
我調試的源代碼,並將其大桶出JsonMediaTypeFormatter
如果屬性標記爲必選,而不是提供拋出一個錯誤。下面的代碼是從JsonMediaTypeFormatter
部分:
// Error must always be marked as handled
// Failure to do so can cause the exception to be rethrown at every recursive level and overflow the stack for x64 CLR processes
jsonSerializer.Error += (sender, e) =>
{
Exception exception = e.ErrorContext.Error;
formatterLogger.LogError(e.ErrorContext.Path, exception);
e.ErrorContext.Handled = true;
}
,這觸發了ModelStateFormatterLogger.LogError
方法,提出了ModelState
裏面的錯誤:
public void LogError(string errorPath, Exception exception)
{
if (errorPath == null)
{
throw Error.ArgumentNull("errorPath");
}
if (exception == null)
{
throw Error.ArgumentNull("exception");
}
string key = ModelBindingHelper.ConcatenateKeys(_prefix, errorPath);
_modelState.AddModelError(key, exception);
}
我仍然無法抑制這種行爲。
在哪個發佈中你得到了這個迴應?我沒有在Webapi 2.1 –