[HttpGet]
public List<UserInfo> GetList()
{
try
{
List<UserInfo> list = new List<UserInfo>();
list.Add(new UserInfo() { UserName = "jay1" });
list[10].id = 1000;
return list;
}
catch (Exception ex)
{
//在webapi中要想抛出异常必须这样抛出,否则只抛出一个默认500的异常
var response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent("{\"result\":\"" + ex.ToString() + "\"}"),
ReasonPhrase = "error"
};
throw new HttpResponseException(response);
}
}

posted on 2015-09-06 21:44  邢帅杰  阅读(1040)  评论(0编辑  收藏  举报