在编写公共函数时,我通常会遵循防御性编程的做法,就像这样。
public long CreateSection(Section section)
{
if (section == null)
throw new ArgumentNullException("section");
var entityId = section.EntityId;
if (entityId == 0)
throw new ArgumentException("To add section, the entity must have already been saved with entityId", "section");
Debug.Assert(section.Id == 0, "If someone give such section, I don't minid to save it.");
...
return section.Id;
}
我将把它包装到ASP.NET Web API 2.x函数中。在ArgumentExceptions上,我想我需要使用HttpStatusCode和ReasonPhase抛出HttpResponseException。
答案 0 :(得分:1)
根据个人意见,
Application_Error
中Global.asax
那样的东西。毕竟,干净的500分解并不是非常漂亮:)