c# - What HttpStatusCode and ReasonPhase in Web API to return for defensive programming? -


when writing public function, follow practices of defensive programming, this.

    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, entity must have been saved entityid", "section");          debug.assert(section.id == 0, "if give such section, don't minid save it.");          ...         return section.id;     } 

i going wrap asp.net web api 2.x function. upon argumentexceptions, think need throw httpresponseexception httpstatuscode , reasonphase.

  1. what httpstatuscode should use?
  2. should overwrite default reasonphase value argument name missing?
  3. what best practice handle exceptions thrown in api controller function , tell clients?

as per personal oppinion,

  1. i use 400 bad request. 500 should avoided cause should handle exceptions , isn't complete breakdown.
  2. yes, it's let consumer know did wrong may correct next request. if service went down in shower of sparks, how know did wrong?
  3. do not throw them. if do, make sure handle them output decent the application_error in global.asax in old asp projects. clean 500 breakdown isn't pretty after :)

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -