title: Custom Type Converter on a Simple Property in ASP.NET Web API
body: <p>In an ASP.NET Web API project I want to encrypt all of entity ID in all responses and decrypt the encrypted values in all the requests.</p>
  
  <p><em>(NOTE: I know how to encrypt/decrypt data, that is not my question.)</em></p>
  
  <p>I think it would be nice if I only decorate the properties that I need to be encrypted/decrypted in responses/requests, with a custom attribute. </p>
  
  <p>This is how I like it to work:</p>
  
  <pre><code>public class Person
  {
    [EncryptDecrypt]
    public int PersonID {get; set;}
  
    public string Name {get; set;}
  
    public IEnumerable&lt;Order&gt; Orders {get; set;}
  }
  
  public class Order 
  {
      [EncryptDecrypt]
      public long OrderID {get; set;}
  
      public string Title {get; set;}
  
      public float Price {get; set;}
  }
  </code></pre>
  
  <p>Then in the Web API method:</p>
  
  <pre><code>// GET: api/persons/xhj$j78dPs (xhj$j78dPs is an encrypted PersonID)    
  
  public Person Get([EncryptDecrypt]int personId)
  {
      // Now, I expect personId to be a normal ID, like: 187356
  
      Person person = _repository.GetPerson(personId);
  
      return person;
  }
  </code></pre>
  
  <p>The desire response for the above Web API is:</p>
  
  <pre><code>{
     "personId": "xhj$j78dPs",
     "name": "Joe Williams",
     "orders": [
        {
           "orderId": "a#jd75mlzed0ihd",
           "title": "Buying a new item",
           "price": 19.99
        }
      ]
   }
  </code></pre>
  
  <p>And this is another example, this time a Web API for PUT verb:</p>
  
  <pre><code>/* PUT Request body: */
  {
     "orderId": "a#jd75mlzed0ihd",
     "title": "Buying a new item - edited",
     "price": 13.00
  }
  </code></pre>
  
  <p>related Web API method:</p>
  
  <pre><code>// PUT: api/persons/xhj$j78dPs/orders/ (xhj$j78dPs is an encrypted PersonID)
  
  public void Put([EncryptDecrypt]int personId, Order editedOrder)
  {
      // I expect personId to be a normal ID, like: 187356
  
      // I expect editedOrder.OrderID to be a normal ID, like: 10000089765
  
      _repository.UpdateOrder(personId, editedOrder);
  }
  </code></pre>
  
  <p>How can I develop the <code>[EncryptDecrypt]</code> attribute?</p>
  
  <p>Is <code>[EncryptDecrypt]</code> should be actually a <a href="http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonConverterAttribute.htm" rel="nofollow">JsonConverter attribute</a>? Or should I develop a custom <a href="http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters" rel="nofollow">Media Formatter</a> or Model Binder or Value Provider or Parameter Binder? I am confused.</p>