I have my local host and a live site. I have a url and if its in localhost the url should go localhost/site/thank_you.aspx and if its live http://mylivesite.com/thank_you.aspx
I have tried this in my code behind...
MyHiddenField.Value = Request.URL + "/thank_you.aspx";
but it returned the page I was on /thank_you.aspx
What am I doing wrong?
Usually, you can access the localhost of any computer through the loopback address 127.0. 0.1. By default, this IP address references a server running on the current device. In other words, when your computer requests the IP address 127.0.
It can be done in two steps: On the server, create a virtual host like forward.mydomain.com with a reverse proxy to some unused port (say, 5000 ). Now create a tunnel so whatever comes at port 5000 on the server is tunneled to your local machine's port 3000 (PC/laptop).
Try this, I even added scheme in too, just in case you go https :)
EDIT: Also added port (Thanks Alex) in order to be super duper super future-proof :)
MyHiddenField.Value = string.Format(
"{0}://{1}{2}/thank_you.aspx",
Request.Url.Scheme,
Request.Url.Host,
Request.Url.IsDefaultPort ? string.Empty : ":" + Request.Url.Port);
EDIT: Another good suggestion by @MikeSmithDev, put it in a function
public string GetUrlForPage(string page)
{
return MyHiddenField.Value = string.Format(
"{0}://{1}{2}/{3}",
Request.Url.Scheme,
Request.Url.Host,
Request.Url.IsDefaultPort ? string.Empty : ":" + Request.Url.Port,
page);
}
Then you can do:
MyHiddenField.Value = GetUrlForPage("thank_you.aspx");
There is a built-in class UriBuilder
var url = Request.Url;
var newurl = new UriBuilder(url.Scheme, url.Host, url.Port, "thank_you.aspx")
.ToString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With