title: Require HTTPS in C# Web API
body: <p>How can I require HTTPS for my C# apicontrollers?</p>
  
  <p>I know that I can add a RequireHTTPSAttribute like this here : <a href="http://www.asp.net/web-api/overview/security/working-with-ssl-in-web-api" rel="nofollow">http://www.asp.net/web-api/overview/security/working-with-ssl-in-web-api</a> but this just adds "HTTPS Required" to the response.</p>
  
  <p>What I would rather have it do, is output json on screen that says, "HTTPS Required" instead of it just being on what of the response headers.</p>
  
  <p>Is this possible?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>This is what I came up with, is there a more elegant way to do it, so I have less repetition of code.</p>
  
  <pre><code>        ResultWrapper&lt;Temp&gt; wrapper = new ResultWrapper&lt;Temp&gt;(Request);
  
          if (Request.RequestUri.Scheme != Uri.UriSchemeHttps)
          {
              wrapper.Error = "Https is Required";
          }
          else
          {
              wrapper.RequestUrl = Request.RequestUri.ToString();
  
              wrapper.Results.Url = "http://www.google.com";
          }
  
          return wrapper;
  </code></pre>
  
  <p><strong>Second Edit</strong></p>
  
  <p>I found a better way, you can edit the Content stream being sent back in the response and send back any class you want serialized as Json, like this :</p>
  
  <pre><code> Content = new StringContent(Json.Encode(wrapper))
  </code></pre>