Tom Knowlton
asked on
Having to build URL from scratch vs localhost:<<PORT NUMBER>> in IDE test environment
Currently I am having to switch my code back and forth for deployment vs local debugging:
Deployment:
public static string PerformRedirect(string usehttps, string servername, string serverport, string webpage)
{
string urltouse = "";
if (usehttps == "YES")
{
//urltouse = "https://" + servername + ":" + serverport + "/" + webpage;
urltouse = "https://" + servername + "/" + webpage;
}
else
{
//urltouse = "http://" + servername + ":" + serverport + "/" + webpage;
urltouse = "http://" + servername + "/" + webpage;
}
return urltouse;
}
localhost development and debugging:
public static string PerformRedirect(string usehttps, string servername, string serverport, string webpage)
{
string urltouse = "";
if (usehttps == "YES")
{
urltouse = "https://" + servername + ":" + serverport + "/" + webpage;
//urltouse = "https://" + servername + "/" + webpage;
}
else
{
urltouse = "http://" + servername + ":" + serverport + "/" + webpage;
//urltouse = "http://" + servername + "/" + webpage;
}
return urltouse;
}
Is there a way to set this up so I do not have to remember to change my code before publishing -- or -- not have to remember to change it back for testing on localhost?
MUST localhost always include a port number? Is this so it does not conflict with IIS on my local machine?
Deployment:
public static string PerformRedirect(string usehttps, string servername, string serverport, string webpage)
{
string urltouse = "";
if (usehttps == "YES")
{
//urltouse = "https://" + servername + ":" + serverport + "/" + webpage;
urltouse = "https://" + servername + "/" + webpage;
}
else
{
//urltouse = "http://" + servername + ":" + serverport + "/" + webpage;
urltouse = "http://" + servername + "/" + webpage;
}
return urltouse;
}
localhost development and debugging:
public static string PerformRedirect(string usehttps, string servername, string serverport, string webpage)
{
string urltouse = "";
if (usehttps == "YES")
{
urltouse = "https://" + servername + ":" + serverport + "/" + webpage;
//urltouse = "https://" + servername + "/" + webpage;
}
else
{
urltouse = "http://" + servername + ":" + serverport + "/" + webpage;
//urltouse = "http://" + servername + "/" + webpage;
}
return urltouse;
}
Is there a way to set this up so I do not have to remember to change my code before publishing -- or -- not have to remember to change it back for testing on localhost?
MUST localhost always include a port number? Is this so it does not conflict with IIS on my local machine?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
tgerbert:
Will your code still work if PORT number is being inserted by Cassini?
Will your code still work if PORT number is being inserted by Cassini?
ASKER
tgerbert:
And...will it run the same in localhost vs deployed live?
And...will it run the same in localhost vs deployed live?
Yes, and yes. It uses whatever the current URL is and only changes the http(s) part and the path part, thus if the website is running under Cassini on port 50083 (e.g. the url is http://localhost:50083/yoursite/default.aspx) and you call GetRedirectURL("/someFolde r/SomePage .aspx", true) it would return https://localhost:50083/someFolder/Somepage.aspx
...also note that it always constructs an aboslute path, meaning if the current URL is http://localhost/yourSite/Default.aspx and you call GetRedirectURL("page2.aspx ", true) the result will be https://localhost/page2.aspx, and not https://localhost/yourSite/page2.aspx
Have a look around in the documentation at the Uri and UriBuilder classes as I'm sure you can be clever enough to fix that if it's necessary.
Have a look around in the documentation at the Uri and UriBuilder classes as I'm sure you can be clever enough to fix that if it's necessary.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Gah.........what did I press?
This is solved and the points go to tgerbert (450) and cart tawn (50) grade: A
This is solved and the points go to tgerbert (450) and cart tawn (50) grade: A
ASKER
thx
But it looks like this function's job is to switch between HTTP and HTTPS URLs on the same server, in the same site?
In that case, you could use a function like below which will take whatever server name and port number the current URL is using and substitute in a new scheme part of the URL (http or https) and path.
Open in new window