【发布时间】:2012-07-17 13:23:44
【问题描述】:
我一直在研究使用 .net Web API 编写 Web 服务,但我看到了两种不同的发送错误消息的方法。第一个是返回一个带有适当HttpStatusCode 集的HttpResponseMessage,如下所示:
public HttpResponseMessage<string> Get()
{
...
// Error!
return new HttpResponseMessage<string>(System.Net.HttpStatusCode.Unauthorized);
}
另一种方法是直接抛出HttpResponseException,像这样:
public Person Get(int id)
{
if (!_contacts.ContainsKey(id))
{
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, String.Format("Contact {0} not found.", id)));
}
return _contacts[id];
}
使用其中任何一种都有哪些优点/缺点?在可扩展性和性能方面,哪一个比另一个更好?
【问题讨论】:
-
啊,我没看到那个帖子。这确实有很大帮助!谢谢:)