I'm aware that there are easier ways to do this and believe me, I've tried them. I'm of course open to any suggestions =). You don't need to read the whole code, just the part that says where the problem lies. Also, I'm debbugging perl style so you guys can see. Oh and did I mention that on my development environment everything works as intended?
Here's the code:
string GetPortalAlias()
{
String myURL2 = Request.Url.ToString();
URLLabel.Text = "Original Request.Url.ToString() returned: \"" + myURL2 + "\"";
string myURL = string.Copy(myURL2);
URLLabel.Text = "Copying it to myURL, it's now: \"" + myURL + "\"";
myURL = myURL.ToLower().Trim();
URLLabel.Text += "<br>Trimming and ToLower myURL.<br>The new url is \"" + myURL + "\"" + "<br>";
myURL = myURL.Replace(":80", "");
URLLabel.Text += "Replacing the \":80\".<br> The new url is\"" + myURL + "\"<br>";
//***HERE LIES THE PROBLEM***
myURL = myURL.Replace("http://", "");
URLLabel.Text += "Replacing the \"http://\".<br> The new url is\"" + myURL + "\"<br>";
//***PROBLEM ENDS***
myURL = myURL.Remove(myURL.IndexOf("/"));
URLLabel.Text += "Removing everything after the \"/\"." + "<br> The new url is \"" + myURL + "\"<br>";
URLLabel.Text += "<br>GetPortalAlias Returning \"" + myURL + "\"";
return myURL;
}
Believe it or not, the output produced in the webpage is this:
Copying it to myURL, it's now: "http://sar.smg.com.ar/Default.aspx?TabID=912" Trimming and ToLower myURL. The new url is "http://sar.smg.com.ar/default.aspx?tabid=912" Replacing the ":80". The new url is"http://sar.smg.com.ar/default.aspx?tabid=912" Replacing the "http://". The new url is"intranetqa/default.aspx?tabid=912" Removing everything after the "/". The new url is "intranetqa" GetPortalAlias Returning "intranetqa"
So... for some reason whenever it reaches the replace section it mysteriously mutates to start with "intranetqa" instead of "sar.smg.com.ar". "intranetqa" is our default hostname. CHANGING OR TAKING AWAY ANY CHARACTER OF HTTP:// IN ANY WAY MUTATES THE STRING.
I do a string.copy
because I'm aware that if two strings are equal the compiler stores them in the same place therefore I wanted to prevent errors. Taking those lines away and use Request.Url.ToString()
tomyURL
directly does nothing at all. They were just a test to see if that worked.
Here's a list of the things I've tried:
- All combinations of
string
/String
, none worked. - I've tried
Request.Host.Url
and it just gave me "intranetqa". - I've used
Request.Url.AbsoluteUri
and that's why I have the replace :80 line. - USING THE
.tochararray
FUNCTION GIVES ME BACK THE INTRANETQA THING - myURL = myURL.Substring(6) gives back the intranetqa thing.
- string.Contains("sar.smg.com.ar") gives back false.
I believe the trick lies around here:
Uri uriAddress1 = Request.Url;
and"The parts are <br>" + "Part 1: " + uriAddress1.Segments[0] + "<br>Part 2: " + uriAddress1.Segments[1];
Gives Part1 : "/" and Part 2: "Default.aspx". Trying to access part 3 (index 2) gives an exception. The request.url does not have the first part, but when I call the ToString() method, it does have like a "fake" first part
";` after the problematic line. – Henrik Oct 16 '12 at 15:12