Search code examples
asp.net-web-apiputidempotent

Why put method is not always idempotent in Asp Net Web Api?


As far as I know put is idempotent, i.e., applying this method multiple times will be the same as apply it one time. Please consider the following piece of code:

public class ValuesController : ApiController
{
    private static List<string> _list = new List<string>();

    public void Put([FromBody]string value)
    {
        _list.Add(value);
    }

}

Clearly as you can see from this example, Put method is not being idempotent here and applying it multiple times will result in _list having multiple values. Additionally I could implement other idempotent methods like get or delete in the similar manner. How come put method is always idempotent then?


Solution

  • PUT should be idempotent. If your implementation of PUT is not idempotent, then it's in violation of the HTTP specification.

    However, that doesn't mean that you are prevented from writing non-conforming implementations.

    Technically this is no longer HTTP