title: MVC RequireHttps and redirect if not https
body: <p>I've read thru many of the questions on ASP.NET MVC <code>[RequireHttps]</code> - but can't find the answer to this question: </p>
  
  <p>How do you make the <code>[RequireHttps]</code> attribute switch the url to https if it was not https to start with?</p>
  
  <p>I have this code:</p>
  
  <pre><code>public ActionResult DoSomething()
  {
      return View("AnotherAction");
  }
  
  [RequireHttps]
  public ActionResult AnotherAction()
  {
      return View();
  }
  </code></pre>
  
  <p>But I get an error saying: "The requested resource can only be accessed via SSL."</p>
  
  <p>The MVC futures project has a similar attribute <code>[RequireSsl(Redirect = true)]</code>.  But that is outdated now ... What is the equivalent in MVC 2?</p>
  
  <p>When someone types in the URL <a href="http://example.com/home/dosomething" rel="nofollow noreferrer">http://example.com/home/dosomething</a> OR the url <a href="http://example.com/home/anotheraction" rel="nofollow noreferrer">http://example.com/home/anotheraction</a>, I need them to be automatically redirected to the url http<strong>s</strong>://example.com/home/anotheraction</p>
  
  <p>EDIT this is the sequence of events:</p>
  
  <p>The URL <a href="http://example.com/home/dosomething" rel="nofollow noreferrer">http://example.com/home/dosomething</a> is called from another website. They redirect their users to this url (with a response.redirect or similar).  </p>
  
  <p><code>DoSomething()</code> then tries to return <code>AnotherAction()</code>, but fails with the error message "The requested resource can only be accessed via SSL."</p>