Solved

Having to build URL from scratch vs localhost:<<PORT NUMBER>> in IDE test environment

Posted on 2010-11-22
10
953 Views
Last Modified: 2012-05-10
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?
0
Comment
Question by:Tom Knowlton
  • 5
  • 4
10 Comments
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 50 total points
ID: 34190765
If you're running it from within Visual Studio then you are probably using Cassini which, by default, uses a random free port when it runs. You could configure Visual Studio to run under IIS instead which would eliminate the port number when running locally.

Where are you getting the values that you pass to "PerformRedirec()"?
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34190972
If you're redirecting to another page on the same server you can just do Response.Redirect("someOtherPage.aspx") without needing to worry about the protocol, server name and port number.

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.

private string GetRedirectURL(string pageName, bool ssl)
{
	string scheme = ssl ? "https" : "http";

	UriBuilder newUrl = new UriBuilder();
	newUrl.Scheme = scheme;
	newUrl.Host = Request.Url.Host;
	if (!Request.Url.IsDefaultPort)
		newUrl.Port = Request.Url.Port;
	newUrl.Path = pageName;
	return newUrl.ToString();
}

Open in new window

0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 450 total points
ID: 34190982
Or, better yet:

private string GetRedirectURL(string pageName, bool ssl)
{
	string scheme = ssl ? "https" : "http";

	UriBuilder newUrl = new UriBuilder(Request.Url);
	newUrl.Scheme = scheme;
	newUrl.Path = pageName;
	return newUrl.ToString();
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:Tom Knowlton
ID: 34191963
tgerbert:

Will your code still work if PORT number is being inserted by Cassini?
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 34191975
tgerbert:

And...will it run the same in localhost vs deployed live?
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34192015
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("/someFolder/SomePage.aspx", true) it would return https://localhost:50083/someFolder/Somepage.aspx
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34192059
...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.
0
 
LVL 5

Assisted Solution

by:Tom Knowlton
Tom Knowlton earned 0 total points
ID: 34192204
seems to be working....points are yours...thx!!!!!!!!!
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 34192231
Gah.........what did I press?

This is solved and the points go to tgerbert (450) and cart tawn (50) grade:  A
0
 
LVL 5

Author Closing Comment

by:Tom Knowlton
ID: 34221298
thx
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question