title: encoding returnUrl query string in ASP.NET MVC
body: <p>I'm trying to pass <code>ReturnUrl</code> query string to my login page. I have a partial Login view (kind of like a login box) and a login view. Right now, I have the following line in my partial view:  </p>
  
  <pre><code>using (Html.BeginForm("Login"
                      , "Account"
                      , new { returnUrl=HttpUtility.UrlEncode(
                                        Request.QueryString["ReturnUrl"]) 
                                        ?? HttpUtility.UrlEncode(Request.Path)
                             }
                      , FormMethod.Post))
  </code></pre>
  
  <p>When I click on my partial view's submit button(e.g. from 
  <code>http://localhost:12345/Product/1234/Product1</code>),</p>
  
  <p>I'll get redirected to my login page with the following url:  </p>
  
  <pre><code>http://localhost:12345/Account/Login?returnUrl=%2FProduct%2F1234%2FProduct1  
  </code></pre>
  
  <p>But if I log on, I'll see an http 400 response, because the page is returning to</p>
  
  <p><code>http://localhost:12345/Account/%2fProduct%2f1234%2fproduct1</code>.  </p>
  
  <p>Also, if I type in the wrong credentials, I cause the <code>returnUrl</code> query string to be encoded again thus every <code>%</code> character is converted to <code>%25</code> again!</p>
  
  <p>I should mention if I manually type in </p>
  
  <p><code>http://localhost:12345/Account/Login?returnUrl=/Product/1234/Product1</code> </p>
  
  <p>(without encoding) and log in, I'm successfully redirected to the specified path.</p>
  
  <p>I guess I'm missing something obvious. But couldn't find out what.</p>
  
  <p>Thanks. </p>