9

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:

output

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
26
  • 2
    Did you try turn it off and back on again?
    – deanvmc
    Oct 16, 2012 at 14:44
  • This is probably not important, but I notice you are using a capitol S for the first string, and not the second, replace the S with an s on the first one and try it?
    – Serdalis
    Oct 16, 2012 at 14:45
  • 1
    @Serdalis String/string are exactly the same. It seems as though your host name is being replaced by the local host name.
    – James
    Oct 16, 2012 at 14:47
  • 3
    1. The screen shot is very hard to read, copying the text out into a <pre> in your question would help. 2. If you cannot re-create this in a fresh console app (or similar) then it is something else in the web app (eg. something else is modifying URLLabel.Text). Perhaps seeing what a debugger shows before and after the problematic replace?
    – Richard
    Oct 16, 2012 at 14:48
  • 1
    What happens after method GetPortalAlias was called? Might it be that some other parts of the program work with text of URLLabel and alter it anyhow? Try introducing a new label and using it instead of URLLabel to make sure nothing happens with its value.
    – Andrei
    Oct 16, 2012 at 14:58

5 Answers 5

11

Between your browser and the server are a reverse proxy and an output re-writer. These may be the same component, or separate components.

The URL your server actually sees is always of the form http://intranetqa/default.aspx?tabid=912 (after the reverse proxy/URL re-writer has intercepted the request).

The output your server produces is actually like:

Copying it to myURL, it's now: "http://intranetqa/Default.aspx?TabID=912"
Trimming and ToLower myURL.
The new url is "http://intranetqa/default.aspx?tabid=912"
Replacing the ":80".
The new url is"http://intranetqa/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" 

The output re-writer is inspecting the output from your server and doing a replace of http://intranetqa with http://sar.smg.com.ar. Once you strip the http:// off of the front of these strings, it's no longer a match and so replacement no longer occurs.

If you want to know what the original requesting URL/host are, hopefully the reverse proxy either is, or can be configured to, adding an extra header to the request with the original URL.

11
  • This is great!!! But wait... this means that there's no way I can get the original url? Maybe is it stored on a server variable? Also, How can I configure the reverse proxy like that? Ofcourse I'm gonna google it now but if you took this out from a link then please send it to me =). Thanks a lot Damien.
    – Gaspa79
    Oct 16, 2012 at 16:03
  • 1
    @Damieh - No, this wasn't from a link. Just a sudden realisation of a completely different situation that matches all of your symptoms. The reverse proxy is almost certainly hosted on the machine that responds at sar.smg.com.ar - you need to talk to the people who administer that machine. Alternatively, re-write your current method to instead iterate through the Request.Headers property and look at the header names - you may spot one which seems a likely candidate - it may already be there. Oct 16, 2012 at 16:07
  • 2
    So, basically, the string has http://intranetqa in it all along, but whenever it displays that link it's being converted to http://sar.smg.com.ar... up until the "http://" is gone, and it no longer is recognized? Wow. That is a really impressive deduction.
    – Bobson
    Oct 16, 2012 at 16:49
  • 2
    @Bobson - the weird part was, as soon as I could see it, I assumed it was obvious. I was sure that by the time I finished my commute home, someone else would have posted the equivalent answer. Oct 16, 2012 at 17:22
  • 1
    @Jodrell - it may be the url the user typed, it may be a fixed string. Basically, there's a URL re-writer in there that's inspecting and modifying the output content. See e.g. Creating Outbound Rules for URL Rewrite Module for one way it can be done if IIS is doing it. Oct 17, 2012 at 8:30
1

You can try something like this

Uri uriAddress1 = new Uri("http://www.contoso.com/title/index.htm");
Console.WriteLine("The parts are {0}, {1}, {2}", uriAddress1.Segments[0], uriAddress1.Segments[1], uriAddress1.Segments[2]);

Uri.Segments Property

This is better way to handle URIs and their segments.

1
  • Okay this is reaaally strange: Here's what I get out of this. Part 0 is just "/" and Part 1 is Default.aspx. Trying to access part 2 gives an exception. Any ideas?
    – Gaspa79
    Oct 16, 2012 at 15:22
1

Try to use this property instead:

String myURL2 = Request.Url.AbsoluteUri;
1
  • I did that and that's why I have the myURL.Replace(":80",""); line. Because that absoluteUri returns a :80 also.
    – Gaspa79
    Oct 16, 2012 at 15:03
1

Here is an Extension method that I use to pull the SiteRootPath. You should be able to easily adjust it however you need it. You will need access to the HttpContext for what I currently have below, however, you don't sound like you need that.

using System;
using System.Web;

namespace FlixPicks.Web.Extensions
{
    public static class HttpContextExtensions
    {
        public static string SiteRootPath(this HttpContext context)
        {
            if (context == null || context.Request == null) { return null; }

            return context.Request.Url.SiteRootPath(context.Request.ApplicationPath);
        }

        public static string SiteRootPath(this HttpContextBase context)
        {
            return context.Request.Url.SiteRootPath(context.Request.ApplicationPath);
        }

        private static string SiteRootPath(this Uri url, string applicationPath)
        {
            if (url == null) { return null; }

            // Formatting the fully qualified website url/name.
            string appPath = string.Format(
                        "{0}://{1}{2}{3}",
                        url.Scheme,
                        url.Host,
                        url.Port == 80 ? string.Empty : ":" + url.Port,
                        applicationPath);

            // Remove ending slash(es) if one or more exists to consistently return
            // a path without an ending slash.  Could have just as well choosen to always include an ending slash.
            while (appPath.EndsWith("/") || appPath.EndsWith("\\"))
            {
                appPath = appPath.Substring(0, appPath.Length - 1);
            }

            return appPath;
        }
    }
}

Good luck, Tom

2
  • Apparently this is for 4.0 because there's no such thing as Request.Url.SiteRootPath in 3.5 Am I right?
    – Gaspa79
    Oct 16, 2012 at 15:45
  • 1
    @Damieh SiteRootPath is an Extension method that I included here. You will need to include the namespace for this extension class in the spot you are using it.
    – Thomas
    Oct 23, 2012 at 18:48
1

Don't you want to achieve part of what is done here?

Something like

string host = Request.Url.IsDefaultPort ?
    Request.Url.Host :
    Request.Url.Authority;

If you want to persist with the old method change it like this

string GetPortalAlias()
{
    var rawUrl = Request.Url.ToString();
    var lowerTrimmedUrl = rawUrl.ToLower().Trim();
    var withoutPortUrl = lowerTrimmedUrl.Replace(":80", "");
    var withoutProtocolUrl = withoutPortUrl.Replace("http://", "");
    var justHostUrl = withoutProtocolUrl.Remove(myURL.IndexOf("/"));

    var evolution = new StringBuilder();
    evolution.AppendFormat(
        "{0}<br>", 
        HttpUtility.HtmlEncode(rawUrl));
    evolution.AppendFormat(
        "{0}<br>",
        HttpUtility.HtmlEncode(lowerTrimmedUrl));
    evolution.AppendFormat(
        "{0}<br>",
        HttpUtility.HtmlEncode(withoutPortUrl));
    evolution.AppendFormat(
        "{0}<br>",
        HttpUtility.HtmlEncode(withoutProtocolUrl));
    evolution.AppendFormat(
        "{0}<br>",
        HttpUtility.HtmlEncode(justHostUrl));

    URLLabel.Text = evolution.ToString();
    return justHostUrl;
}

So you can see whats going on.

2
  • Yes and no, I want to served-sided get the host that the user actually typed. Request.Url.Host will give me the default host (intranetqa), which is not necessarily what the user typed (sar.smg.com.ar)
    – Gaspa79
    Oct 16, 2012 at 15:20
  • @Damieh I hope the encoding would fix any display issues you have.
    – Jodrell
    Oct 16, 2012 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.