有没有办法一般处理 404(找不到资源),还是我必须为每个操作编写逻辑?我目前正在做的一个简单示例:
//single-read
public HttpResponseMessage Get(Guid guid)
{
School school = new School(guid);
if (school == null)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
//bonus-question: would it be better to instead: throw new HttpResponseException(HttpStatusCode.NotFound);
}
//bonus-bonus-question: what is the benefit of using a typed response?
return Request.CreateResponse<School>(HttpStatusCode.OK, school);
}
如果可能的话,我希望在其他地方处理“未找到”逻辑,这样我就不需要为每个操作重复检查那几行。例如,如果它可以简化为:
//single-read
public HttpResponseMessage Get(Guid guid)
{
School school = new School(guid);
return Request.CreateResponse<School>(HttpStatusCode.OK, school);
}