title: how can mvc return Unauthorized code without redirecting to LogIn view
body: <p>My MVC web application serves two types of users.</p>
  
  <ul>
  <li>First one over standard web browser;</li>
  <li>Second one over REST returning only JSON data.</li>
  </ul>
  
  <p>Additionally,</p>
  
  <ul>
  <li>Both require Authentication and authorization;</li>
  <li>Both scenarios are differentiated based on the route so that I know what content to serve.</li>
  </ul>
  
  <p>When users access the application, if they are not logged in, the application should react differently.</p>
  
  <ol>
  <li>In the first case it should return the default LogIn page (this is fine).</li>
  <li>In the second case it should return a 401 Unauthorized code only.</li>
  </ol>
  
  <p>I'm used to working with WCF REST service where I could raise an exception like this:</p>
  
  <pre><code>throw new WebProtocolException(System.Net.HttpStatusCode.Unauthorized, exc.Message, exc);
  </code></pre>
  
  <p>and receive a 401 message. The problem with the same approach within MVC when I put the <code>statusCode</code> like this:</p>
  
  <pre><code>HttpContext.Response.StatusCode = (Int32)HttpStatusCode.Unauthorized
  </code></pre>
  
  <p>it always redirects to the LogIn page.</p>
  
  <p>How can I do this?</p>
  
  <p>I've tried overriding the <code>AuthorizeAttribute</code> and handling the <code>OnAuthorization</code> function, but still as soon as I set the <code>statusCode</code> to 401 it gets redirected to the LogIn page.</p>