Tuesday, July 22, 2014

OPTIONS and HEAD in the ASP.NET Web API

The verbs POST, GET, PUT, and DELETE seem more or less, in my impression, to correspond to the C, the R, the U, and the D in CRUD (create, read, update, and delete) respectively in Roy Fielding's vision for REST. But there are a few other request methods one may utilize per this, and two of them seem to work with the ASP.NET Web API. I did some experimenting just now (in each case trying to specify an item below as an all-caps verb in a jQuery .ajax type parameter while reaching out from the jQuery .ajax implementation to an ApiController method with the same name in Pascal Case which returns a string) and here is what I found:

  1. OPTIONS works perfectly. This is legit! Variables may be handed to the method and one may get a result back. There are no errors.
  2. HEAD lets you reach the ApiController method and hand variables into there, (I know because I set a breakpoint) but you will not get a result back at the jQuery side. This suggests that is perfectly normal and that HEAD is not meant to return stuff in REST. There is not an error in the console on the JavaScript side. The result just comes back as undefined if you try to catch one.
  3. TRACE does nothing. I set a breakpoint in the ApiController and I could not reach it. No error is written to the console and there is also not an ASP.NET exception thrown. We just silently fail.
  4. CONNECT does nothing too. No errors are thrown in either C# or JavaScript, but no code on the C# is ever reached and thus no result is ever returned. This type and the last one do the same sort of nothing.
  5. AMPUTATE throws a "405 (Method Not Allowed)" error up to the console. You cannot invent your own verb.

Addendum 12/17/2014: Some of the verbs which seems to do nothing may just be turned off at the web server (Cassini). I dunno.

No comments:

Post a Comment