我试图弄清楚如何以标准方式返回我的数据。我的意思是当我返回 json 或 xml 时,最好为所有内容(成功和错误)提供一种格式。
假设我有以下 json 结果。
{
"person": {
"id": 12345,
"firstName": "John",
"lastName": "Doe",
"phones": {
"home": "800-123-4567",
"work": "888-555-0000",
"cell": "877-123-1234"
},
"email": [
"jd@example.com",
"jd@example.org"
],
"dateOfBirth": "1980-01-02T00:00:00.000Z",
"registered": true,
"emergencyContacts": [
{
"name": "",
"phone": "",
"email": "",
"relationship": "spouse|parent|child|other"
}
]
}
}
这一切都很好,但现在如果出现验证错误会发生什么
我可以使用内置方法 CreateErrorResponse
{
"Message": "The request is invalid.",
"ModelState": {
"item": [
"Required property 'Name' not found in JSON. Path '', line 1, position 14."
],
"item.Name": [
"The Name field is required."
],
"item.Price": [
"The field Price must be between 0 and 999."
]
}
}
*是的,我知道数据没有意义并且有所不同,但数据仅与结构无关。
现在如果我有错误会发生什么,在这种情况下它有一个自定义错误代码。
我可以返回这样的东西(使用 HttpError)
{
"Message": "My custom error message",
"CustomErrorCode": 37
}
现在你可以看到我有 3 种不同格式的 json 回来了。现在在客户端我必须这样做
- 检查 HttpStatusCode
- 如果是 200,那么在这种情况下使用 Person 的格式解析 json。
- 如果 400 则可能是验证错误或服务器错误。
- 如果发现客户错误,则使用该格式,否则使用 modlestate。
我一直在使用foursquare,似乎他们总是将相同的格式返回给用户,但我不知道如何在我这样做时获得相同的东西。
{
"meta": {
"code": 200,
...errorType and errorDetail...
},
"notifications": {
...notifications...
},
"response": {
...results...
}
}
我想做类似的事情
这将是一个好的要求。
{
"meta": {
"code": 200,
"ModelState": {}
},
"response": {
"person": {
"id": 12345,
"firstName": "John",
"lastName": "Doe",
"phones": {
"home": "800-123-4567",
"work": "888-555-0000",
"cell": "877-123-1234"
},
"email": [
"jd@example.com",
"jd@example.org"
],
"dateOfBirth": "1980-01-02T00:00:00.000Z",
"registered": true,
"emergencyContacts": [
{
"name": "",
"phone": "",
"email": "",
"relationship": "spouse|parent|child|other"
}
]
}
}
}
服务器错误看起来像这样
{
"meta": {
"code": 500,
"message": "this is a server error",
"ModelState": {}
},
"response": {}
}
验证看起来像这样
{
"meta": {
"code": 400,
"message": "validation errors",
"Message": "The request is invalid.",
"ModelState": {
"item": [
"Required property 'Name' not found in JSON. Path '', line 1, position 14."
],
"item.Name": [
"The Name field is required."
],
"item.Price": [
"The field Price must be between 0 and 999."
]
}
},
"response": {}
}
但就像我说的不确定如何做这样的事情,也不是 100% 确定这仍然是最好的方法。至少它应该是一种格式呢?
编辑 @Erik Philips
当我只做 asp.net mvc 项目时,我会做这样的事情。
public readonly IValidation validation;
public PersonService(IValidation validation)
{
this.validation = validation;
}
public Person GetPerson(int id)
{
try
{
return FindPerson(id);
}
catch(Exception ex)
{
//log real error with elmah
validation.addError("internal", "Something went wrong");
}
}
public class PersonController
{
public readonly IPersonService personService;
public PersonController(IPersonService personService)
{
this.personService = personService;
}
public ActionResult GetPerson(int id)
{
personService.GetPerson(id);
if(personService.Validation.IsValid)
{
// do something
}
else
{
// do something else
}
return View();
}
}
我喜欢你的设置方式,但我想保持这种方式。我不认为我可以使用界面,但我正在考虑这样的事情
public PersonService()
{
}
public ResponseResult<Person> GetPerson(int id)
{
var result = ResponseResult<Person>();
try
{
return FindPerson(id);
}
catch(Exception ex)
{
result.Errorcode = 200;
result.Msg = "Failed";
}
}
public class PersonController
{
public readonly IPersonService personService;
public PersonController(IPersonService personService)
{
this.personService = personService;
}
public HttpResponseMessage GetPerson(int id)
{
var result = personService.GetPerson(id);
if(result.isValid)
{
Request.CreateResponse<ResponseResult<Person>>(HttpStatusCode.OK, result);
}
Request.CreateResponse<ResponseResult<Person>>(HttpStatusCode.BadRequest, result);
}
}