【发布时间】:2016-03-06 12:10:59
【问题描述】:
我有 webapi,它返回 JArray。 有什么方法可以发送带有我选择的一些状态码的响应(例如 422, 4XX )?
//GET api/UserControl/GetUserName
public JArray GetUserName()
{
JArray json = new JArray();
try
{
string UserID= getUserID();
if (!string.IsNullOrEmpty(UserID) || UserID== "None Was found")
{
var result = JsonConvert.SerializeObject(donorRep.GetUserFullName(UserID), Formatting.None,
new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
try
{
json = JArray.Parse(result);
}
catch
{
json.Add(result.ToString());
}
}
else
{
json.Add("There was an issue while retrieving your UserID.");
}
}
catch (JsonSerializationException ex)
{
json.Add("There was an issue while retrieving your IDSID. Please contact support");
}
return json;
}
例如,如果这里比 UI 中出现错误:
json.Add("There was an issue while retrieving your UserID");
【问题讨论】:
标签: json asp.net-web-api json.net