我想编写Web API进行应用程序开发人员,我要像下面如何处理,我在asp.net网页API想要的结果
当异常样本的API结果:
{
"StatusCode": "0",
"Message": "There's exception when calling web api"
}
正常:json字符串中的结果是web api操作中的返回类型。
{
"StatusCode": "1",
"Message": "Action completed successful",
"Result": {}
}
如果操作为:
public DemoController : ApiController
{
public class DemoModel
{
public string X {get;set;}
public int Y {get;set;}
}
[HttpGet]
public DemoModel GetModel(int id)
{
return new DemoModel() { X = "Demo return string" , Y = 1234};
}
}
JSON字符串应该是成功地调用动作时,下面的示例。
{
"StatusCode": "1",
"Message": "Action completed successful",
"Result": {
"X": "Demo return string",
"Y": 1234
}
}
,当异常,应该是:
{
"StatusCode": "0",
"Message": "There's exception when calling web api"
}
因此,应用程序开发人员可以在web API帮助页面的返回类型的详细信息。
那是容易实现的?以及如何做(没有细节,只是逻辑,还详细越好。)
感谢大家!
返回该对象作为结果'return this.Ok(yourObject);' – Fabio
@Fabio然后返回类型不匹配方法声明,对吧?那么什么是合适的返回类型? – mason
返回类型将IHttpActionResult和客户端'Result'值可以基于'StatusCode'使用 – Fabio