Check if 2 URLs are equal
<p>Is there a method around that tests if 2 URLs are equal, ie point to the same place.
I am not talking about 2 URLs with different domain names pointing to the same IP address but for example 2 URLs that point to the same .aspx page:</p>

<ul>
<li><a href="http://mysite.com/Products/Default.aspx?A=B&amp;C=D&amp;E=F">http://mysite.com/Products/Default.aspx?A=B&amp;C=D&amp;E=F</a></li>
</ul>

<p>is equal to these:</p>

<ul>
<li><a href="http://mysite.com/Products/Default.aspx">http://mysite.com/Products/Default.aspx</a></li>
<li><a href="http://mysite.com/Products/">http://mysite.com/Products/</a></li>
<li>~/Products/Default.aspx</li>
<li>~/Products/</li>
</ul>

<p>Note/assumtions</p>

<ol>
<li>QueryString Values are Ignored</li>
<li>ASP.NET (Pref C#)</li>
<li>Default.aspx is the default page</li>
</ol>

<p><strong>----UPDATE----</strong></p>

<p>This is a very crude method that tests a URL to see if matches the current URL:
I tried the creating a new Uri() with both the local and check URLs but dont know that works and went down the string checking avenue.
The implemenation of the SiteMapProvider skips this step if the URL starts with "Http" as this assumes an external URL. Since I have an SaaS framework that will always ensure relative paths (as these can be on different subdomains) it easier to strip things down.</p>

<p>Any comments on optimization? I guess for a start we can pass in a variable containing the current URL? Not sure of the overhead of calling HttpContext.Current.Request.Url.LocalPath many times?</p>

<pre><code>    /// &lt;summary&gt;
    /// Assumes URL is relative aspx page or folder path
    /// &lt;/summary&gt;
    /// &lt;param name="url"&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public static bool CurrentURLMatch(string url)
    {
        string localURL = HttpContext.Current.Request.Url.LocalPath;
        if (HttpContext.Current.Request.Url.Host == "localhost")
        {
            localURL = localURL.Substring(localURL.IndexOf('/') + 1);
            localURL = localURL.Substring(localURL.IndexOf('/'));
        }
        string compareURL = url.ToLower();

        // Remove QueryString Values
        if (localURL.Contains("?"))
        {
            localURL = localURL.Split('?')[0];
        }

        if (compareURL.Contains("?"))
        {
            compareURL = compareURL.Split('?')[0];
        }

        if (localURL.Contains("#"))
        {
            localURL = localURL.Split('#')[0];
        }
        if (compareURL.Contains("?"))
        {
            compareURL = compareURL.Split('#')[0];
        }

        // Prepare End of Local URL
        if (!localURL.Contains("aspx"))
        {
            if (!localURL.EndsWith("/"))
            {
                localURL = String.Concat(localURL, "/");
            }
        }

        // Prepare End of Compare URL
        if (!compareURL.Contains("aspx"))
        {
            if (!compareURL.EndsWith("/"))
            {
                compareURL = String.Concat(localURL, "/");
            }
        }

        if (localURL.EndsWith(@"/"))
        {
            localURL = String.Concat(localURL, "Default.aspx");
        }

        if (compareURL.EndsWith(@"/"))
        {
            compareURL = String.Concat(compareURL, "Default.aspx");
        }

        if (compareURL.Contains(@"//"))
        {
            compareURL = compareURL.Replace(@"//", String.Empty);
            compareURL = compareURL.Substring(compareURL.IndexOf("/") + 1);
        }

        compareURL = compareURL.Replace("~", String.Empty);

        if (localURL == compareURL)
        {
            return true;
        }

        return false;
    }
</code></pre>
