title: Check if 2 URLs are equal
body: <p>Is there a method 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://example.com/Products/Default.aspx?A=B&amp;C=D&amp;E=F" rel="nofollow noreferrer">http://example.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://example.com/Products/Default.aspx" rel="nofollow noreferrer">http://example.com/Products/Default.aspx</a></li>
  <li><a href="http://example.com/Products/" rel="nofollow noreferrer">http://example.com/Products/</a></li>
  <li><code>~/Products/Default.aspx</code></li>
  <li><code>~/Products/</code></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 creating a <code>new Uri()</code> with both the local and check URLs but dont know that works and went down the string checking avenue.
  The implementation of the SiteMapProvider skips this step if the URL starts with &quot;HTTP&quot; as this assumes an external URL. Since I have a 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 <code>HttpContext.Current.Request.Url.LocalPath</code> many times?</p>
  <pre class="lang-cs prettyprint-override"><code>/// &lt;summary&gt;
  /// Assumes URL is relative aspx page or folder path
  /// &lt;/summary&gt;
  /// &lt;param name=&quot;url&quot;&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 == &quot;localhost&quot;)
      {
          localURL = localURL.Substring(localURL.IndexOf('/') + 1);
          localURL = localURL.Substring(localURL.IndexOf('/'));
      }
      string compareURL = url.ToLower();
  
      // Remove QueryString Values
      if (localURL.Contains(&quot;?&quot;))
      {
          localURL = localURL.Split('?')[0];
      }
  
      if (compareURL.Contains(&quot;?&quot;))
      {
          compareURL = compareURL.Split('?')[0];
      }
  
      if (localURL.Contains(&quot;#&quot;))
      {
          localURL = localURL.Split('#')[0];
      }
      if (compareURL.Contains(&quot;?&quot;))
      {
          compareURL = compareURL.Split('#')[0];
      }
  
      // Prepare End of Local URL
      if (!localURL.Contains(&quot;aspx&quot;))
      {
          if (!localURL.EndsWith(&quot;/&quot;))
          {
              localURL = String.Concat(localURL, &quot;/&quot;);
          }
      }
  
      // Prepare End of Compare URL
      if (!compareURL.Contains(&quot;aspx&quot;))
      {
          if (!compareURL.EndsWith(&quot;/&quot;))
          {
              compareURL = String.Concat(localURL, &quot;/&quot;);
          }
      }
  
      if (localURL.EndsWith(@&quot;/&quot;))
      {
          localURL = String.Concat(localURL, &quot;Default.aspx&quot;);
      }
  
      if (compareURL.EndsWith(@&quot;/&quot;))
      {
          compareURL = String.Concat(compareURL, &quot;Default.aspx&quot;);
      }
  
      if (compareURL.Contains(@&quot;//&quot;))
      {
          compareURL = compareURL.Replace(@&quot;//&quot;, String.Empty);
          compareURL = compareURL.Substring(compareURL.IndexOf(&quot;/&quot;) + 1);
      }
  
      compareURL = compareURL.Replace(&quot;~&quot;, String.Empty);
  
      if (localURL == compareURL)
      {
          return true;
      }
  
      return false;
  }
  </code></pre>