[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);
}
}