Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request.URL for localhost and live site

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?

like image 812
user1269625 Avatar asked Jan 24 '13 20:01

user1269625


People also ask

How do I find my localhost URL?

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.

How do I make my local URL public?

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).


2 Answers

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");
like image 156
mattytommo Avatar answered Oct 27 '22 22:10

mattytommo


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();
like image 36
I4V Avatar answered Oct 27 '22 22:10

I4V