title: InvalidCastException 'HttpResponseMessage' to 'IHttpActionResult' on WebApi2 GET
body: <p>Recently I've been following some WebApi2 tutorials.  I have a situation whereby if a requested GET operation returns data outside of the user's remit, then I need to return a Forbidden code.</p>
  
  <pre class="lang-vb prettyprint-override"><code>Imports System.Net
  Imports System.Net.Http
  Imports System.Web.Http
  
  Namespace Controllers
  
      Public Class MyController
          Inherits ApiController
  
          &lt;Route("Records/{id}")&gt;
          Public Function [Get](id As Int32) As IHttpActionResult
              If Not Remit.IsWithinRemit(id) Then
                  Return Request.CreateErrorResponse(HttpStatusCode.Forbidden, "This data is not within your remit")
              Else
                  Dim r As New CustomObject(id)
                  Return Ok(r)
              End If
          End Function
  
      End Class
  
  End Namespace
  </code></pre>
  
  <p>Unfortunately, although the <code>Ok(r)</code> part works okay, <code>CreateErrorResponse</code> throws an <code>InvalidCastException</code>:</p>
  
  <blockquote>
    <p>Unable to cast object of type 'System.Net.Http.HttpResponseMessage' to type 'System.Web.Http.IHttpActionResult'.</p>
  </blockquote>
  
  <p>I know why the error is happening, but am unsure of the correct approach of how to fix it.
  In other threads, people advise that <code>CreateErrorResponse()</code> is the best approach for WebApi2, but VS creates it's sample GET request returning <code>IHttpActionResult</code>.  Its like stuff doesn't seem to fit together for us newbies at the moment...</p>