title: Set a ASP.NET WEB.API model property to be read-only for consumers of the API?
body: <p>What's the best way of ensuring that a property of a model can only be set by the ASP.NET WEB.API service? To a consumer of the service, that property is read-only.</p>
  
  <p>For example:</p>
  
  <pre><code>public class MyModel
  {
      [Required]
      public string CanBeSetByConsumer { get; set; }
  
      // Can only be set by the service
      public int Id { get; set; }
  }
  
  public class MyModelController : ApiController
  {
      public MyModel Get(int id)
      {
          // get MyModel by Id
          return new MyModel();
      }
  
      public MyModel Post(MyModel myData)
      {
          // save myData to a store and generate an ID
          // return myData with ID populated with a 201 Created
      }
  }
  </code></pre>
  
  <p>In the above example, the consumer of the API can <code>POST</code>:</p>
  
  <pre><code>{
    "CanBeSetByConsumer" : "SomeValue"
  }
  </code></pre>
  
  <p>The consumer can also <code>GET</code>:</p>
  
  <pre><code>{
    "Id" : 1234,
    "CanBeSetByConsumer" : "SomeValue"
  }
  </code></pre>
  
  <p>What I would like to do is return a <code>400 BAD REQUEST</code> if the client <code>POST</code>s:</p>
  
  <pre><code>{
    "Id" : 1234,
    "CanBeSetByConsumer" : "SomeValue"
  }
  </code></pre>