<p>I have a method that where I want to redirect the user back to a login page located at the root of my web application.</p>

<p>I'm using the following code:</p>

<pre><code>Response.Redirect("~/Login.aspx?ReturnPath=" + Request.Url.ToString());
</code></pre>

<p>This doesn't work though.  My assumption was that ASP.NET would automatically resolve the URL into the correct path.  Normally, I would just use</p>

<pre><code>Response.Redirect("../Login.aspx?ReturnPath=" + Request.Url.ToString());
</code></pre>

<p>but this code is on a master page, and can be executed from any folder level.  How do I get around this issue?</p>

[{'answer_id': 31248, 'author': 'Marshall', 'author_id': 1302, 'author_profile': 'https://Stackoverflow.com/users/1302', 'pm_score': -1, 'selected': False, 'text': '<p>What about using</p>\n\n<pre><code>Response.Redirect(String.Format("http://{0}/Login.aspx?ReturnPath={1}", Request.ServerVariables["SERVER_NAME"], Request.Url.ToString()));\n</code></pre>\n'}, {'answer_id': 31437, 'author': 'Duncan Smart', 'author_id': 1278, 'author_profile': 'https://Stackoverflow.com/users/1278', 'pm_score': 7, 'selected': True, 'text': '<blockquote>\n  <p>I think you need to drop the "~/" and replace it with just "/", I believe / is the root</p>\n</blockquote>\n\n<p><strong>STOP RIGHT THERE!</strong> :-) unless you want to hardcode your web app so that it can only be installed at the root of a web site. </p>\n\n<p>"~/" <strong><em>is</em></strong> the correct thing to use, but the reason that your original code didn\'t work as expected is that <code>ResolveUrl</code> (which is used internally by <code>Redirect</code>) tries to first work out if the path you are passing it is an absolute URL (e.g. "**<a href="http://server/" rel="noreferrer">http://server/</a>**foo/bar.htm" as opposed to "foo/bar.htm") - but unfortunately it does this by simply looking for a colon character \':\' in the URL you give it. But in this case it finds a colon in the URL you give in the <code>ReturnPath</code> query string value, which fools it - therefore your \'~/\' doesn\'t get resolved.</p>\n\n<p>The fix is that you should be URL-encoding the <code>ReturnPath</code> value which escapes the problematic \':\' along with any other special characters.</p>\n\n<pre><code>Response.Redirect("~/Login.aspx?ReturnPath=" + Server.UrlEncode(Request.Url.ToString()));\n</code></pre>\n\n<p>Additionally, I recommend that you (or anyone) never use <code>Uri.ToString</code> - because it gives a human-readable, more "friendly" version of the URL - not a necessarily correct one (it unescapes things). Instead use Uri.AbsoluteUri - like so:</p>\n\n<pre><code>Response.Redirect("~/Login.aspx?ReturnPath=" + Server.UrlEncode(Request.Url.AbsoluteUri));\n</code></pre>\n'}, {'answer_id': 52769211, 'author': 'Pavan k ', 'author_id': 5661514, 'author_profile': 'https://Stackoverflow.com/users/5661514', 'pm_score': 0, 'selected': False, 'text': '<p>you can resolve the URL first\nResponse.Redirect("~/Login.aspx);\nand add the parameters after it got resolved.</p>\n'}]
['https://Stackoverflow.com/questions/31221', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1226/']