Web API - Return JSON 对象以及 HTTP 状态
Web API - Return JSON object along with the HTTP Status
我有这个 Web API 函数,如果传入任何无效数据,我希望使用它来 return 错误。我通过使用 Request.CreateResponse 函数来实现,并传递它是错误代码和错误列表。
public HttpResponseMessage Post([FromBody]Lead leadToCreate)
{
// Code to validate the request, and populate errorList which is a List<Error> goes here
if (errorList.Count > 0)
{
// Return list of errors and 304 code
return Request.CreateResponse(HttpStatusCode.NotModified, errorList);
}
else
{
//Validated successfully.
return Request.CreateResponse(HttpStatusCode.Created);
}
}
错误 class 是:
public class Error
{
public int number;
public string description;
public string url;
}
当我在本地主机上使用 Fiddler 发出 POST 请求时,错误列表作为 JSON 响应的一部分输出(参见最后一行):
HTTP/1.1 304 Not Modified
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcU291cmNlIENvZGVcV2ViXEZhc3R0cmFjayBBUElcRmFzdHRyYWNrIEFQSVxhcGlcc2FsZXNFbnF1aXJ5XDFc?=
X-Powered-By: ASP.NET
Date: Wed, 10 Feb 2016 14:49:20 GMT
[{"number":1,"description":"PrivateIndividual.Gender must be valid.","url":""}]
我的问题是,在发布到我的实时服务器后,尽管使用相同的请求数据,但没有输出错误列表,尽管正确的响应代码是 returned:
HTTP/1.1 304 Not Modified
Cache-Control: no-cache Pragma: no-cache Content-Type: application/json; charset=utf-8
Expires: -1 Server: Microsoft-IIS/7.0
X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Wed, 10 Feb
2016 14:52:13 GMT Connection: close X-ScanSafe-Error: F284 Via:
HTTP/1.1 proxy10012
谁能看出为什么这在本地主机上运行良好,但在我的实时服务器上运行不正常?
似乎有一个应用层代理的响应有问题:
2016 14:52:13 GMT Connection: close X-ScanSafe-Error: F284 Via:
HTTP/1.1 proxy10012
看起来这个代理正在阻止 HttpResponse 返回到 fiddler。
我有这个 Web API 函数,如果传入任何无效数据,我希望使用它来 return 错误。我通过使用 Request.CreateResponse 函数来实现,并传递它是错误代码和错误列表。
public HttpResponseMessage Post([FromBody]Lead leadToCreate)
{
// Code to validate the request, and populate errorList which is a List<Error> goes here
if (errorList.Count > 0)
{
// Return list of errors and 304 code
return Request.CreateResponse(HttpStatusCode.NotModified, errorList);
}
else
{
//Validated successfully.
return Request.CreateResponse(HttpStatusCode.Created);
}
}
错误 class 是:
public class Error
{
public int number;
public string description;
public string url;
}
当我在本地主机上使用 Fiddler 发出 POST 请求时,错误列表作为 JSON 响应的一部分输出(参见最后一行):
HTTP/1.1 304 Not Modified
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcU291cmNlIENvZGVcV2ViXEZhc3R0cmFjayBBUElcRmFzdHRyYWNrIEFQSVxhcGlcc2FsZXNFbnF1aXJ5XDFc?=
X-Powered-By: ASP.NET
Date: Wed, 10 Feb 2016 14:49:20 GMT
[{"number":1,"description":"PrivateIndividual.Gender must be valid.","url":""}]
我的问题是,在发布到我的实时服务器后,尽管使用相同的请求数据,但没有输出错误列表,尽管正确的响应代码是 returned:
HTTP/1.1 304 Not Modified
Cache-Control: no-cache Pragma: no-cache Content-Type: application/json; charset=utf-8
Expires: -1 Server: Microsoft-IIS/7.0
X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Wed, 10 Feb
2016 14:52:13 GMT Connection: close X-ScanSafe-Error: F284 Via:
HTTP/1.1 proxy10012
谁能看出为什么这在本地主机上运行良好,但在我的实时服务器上运行不正常?
似乎有一个应用层代理的响应有问题:
2016 14:52:13 GMT Connection: close X-ScanSafe-Error: F284 Via:
HTTP/1.1 proxy10012
看起来这个代理正在阻止 HttpResponse 返回到 fiddler。