【发布时间】:2013-04-19 23:54:12
【问题描述】:
我试图弄清楚如何以标准方式返回我的数据。我的意思是当我返回 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% 确定这仍然是最好的方法。至少应该是一种格式吧?
编辑 @埃里克飞利浦
当我只做 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);
}
}
【问题讨论】:
-
此 API 将在内部(公司内部)还是外部(由第 3 方)使用?
-
仅在内部用于我自己的项目,但我认为任何方式都可以用于任何 webapi 项目(内部或第 3 方)。
-
您还希望这个 API 被 Javascript(jQuery 等)使用吗?
-
我没有看到它被网络消费,因为我需要一个条形码扫描仪并计划编写本机应用程序,而不是说我将使用 javascript 的 phonegap。然而,由于这是一个 web api,我真的在努力学习如何制作它,以便任何东西都可以使用它。我仍然会使用 CreateResponse 或 CreateErrorResponse 并将其包装在另一个代码中,因此 jquery 也应该能够查看它是“ok”、“bad”还是“internal error”。
-
我真的在努力寻找返回数据的最佳实践方式。我认为不必总是检查 3 种格式的 json 来确定刚刚来自服务器的内容是最好的方法。
标签: c# .net json asp.net-web-api