我有此方法TResult GetResource
,我需要添加一个WebException,但它正在寻找返回DeserializeObject
的方法。处理此方法中的错误的最佳方法是什么?
我是否只返回诸如“错误”之类的Deserialize TResult
字符串?
protected TResult GetResource<TResult>(string url)
{
using (var webClient = new WebClient())
{
try
{
var response = Encoding.UTF8.GetString(webClient.DownloadData(url));
//Debug.WriteLine("JSON Response " + response);
return JsonConvert.DeserializeObject<TResult>(response);
}
catch (WebException ex)
{
return JsonConvert.DeserializeObject<TResult>("Error");
}
}
}