title: Code Contracts and ASP.NET MVC Controller properties
body: <p>I am using CodeContracts for the first time, and I have a question regarding it. When I use <code>ModelState</code>, <code>User</code>, <code>Request</code> or <code>Response</code> in my controller code then Code Contracts is saying that I should add a <code>Contract.Requires(this.Request != null)</code> to my code.</p>
  
  <p><strong>Message example:</strong></p>
  
  <blockquote>
    <p>Warning   1   CodeContracts: Missing precondition in an externally visible
    method. Consider adding Contract.Requires(this.ModelState != null);
    for parameter validation </p>
  </blockquote>
  
  <p>I thought I could make those messages disappears from code-contracts by adding:</p>
  
  <pre><code>[ContractInvariantMethod]
  private void ObjectInvariant()
  {
      Contract.Invariant(this.HttpContext != null);
      Contract.Invariant(this.ModelState != null);
      Contract.Invariant(this.Request != null);
      Contract.Invariant(this.Response != null);
      Contract.Invariant(this.RouteData != null);
      Contract.Invariant(this.Server != null);
      Contract.Invariant(this.Session != null);
      Contract.Invariant(this.ViewData != null);
      Contract.Invariant(this.User != null);
      Contract.Invariant(this.Url != null);
  }
  </code></pre>
  
  <p>To my code. But the ObjectInvariant function is checked at runtime before Controller.Initialize is called thus proving all invariants false.</p>
  
  <hr>
  
  <p><strong>TL;DR</strong></p>
  
  <p>Now my questions is, how can I disable those code contracts messages for controller base properties?</p>