title: MVC 4 and Extension Methods with Razor
body: <p>I have created an extension method and have included its namespace in my <code>web.config</code> file. The extension method works fine and is accesed OK by the test code. The problem is, I am still getting an error relating to the namespace not being found.</p>
  
  <p>The ASP .NET error message I am getting is:</p>
  
  <blockquote>
    <p>CS1061: 'System.Uri' does not contain a definition for 'IsCurrentUrl' and no extension method 'IsCurrentUrl' accepting a first argument of type 'System.Uri' could be found (are you missing a using directive or an assembly reference?)</p>
  </blockquote>
  
  <p>Below is the respective code.</p>
  
  <p>Web.config:</p>
  
  <pre><code>&lt;system.web&gt;
      &lt;httpRuntime targetFramework="4.5" /&gt;
      &lt;compilation debug="true" targetFramework="4.5" /&gt;
      &lt;pages&gt;
          &lt;namespaces&gt;
              &lt;add namespace="System.Web" /&gt;
              &lt;add namespace="System.Web.Helpers" /&gt;
              &lt;add namespace="System.Web.Mvc" /&gt;
              &lt;add namespace="System.Web.Mvc.Ajax" /&gt;
              &lt;add namespace="System.Web.Mvc.Html" /&gt;
              &lt;add namespace="System.Web.Routing" /&gt;
              &lt;add namespace="System.Web.WebPages" /&gt;
              &lt;add namespace="MyMainSite2.Library.Extensions" /&gt;
          &lt;/namespaces&gt;
      &lt;/pages&gt;
  &lt;/system.web&gt;
  </code></pre>
  
  <p>Extension method code:</p>
  
  <pre><code>namespace MyMainSite2.Library.Extensions
  {
      public static class UriExtensions
      {
          #region Public Static Methods
  
          public static bool IsCurrentUrl(this Uri uri, string url)
          {
              if (String.IsNullOrWhiteSpace(url))
                  return false;
  
              url = url.Trim().ToLower();
              string absolutePath = uri.AbsolutePath.Trim().ToLower();
  
              if (!url.StartsWith("/") &amp;&amp; absolutePath.StartsWith("/"))
                  absolutePath = absolutePath.Remove(0, 1);
  
              bool match = absolutePath == url;
  
              return match;
          }
  
          #endregion
      }
  }
  </code></pre>
  
  <p>Razor code:</p>
  
  <pre><code>@model MyMainSite2.UI.Web.Models.Shared.TopMenuModel
  
  @foreach (var item in this.Model.Items)
  {
      if(this.Request.Url.IsCurrentUrl(item.Url)) // this line is failing because UriExtensions.IsCurrentUrl is not being found
      {
          @:&lt;li class="current"&gt;
      }
      else
      {
          @:&lt;li&gt;
      }
  
      @:&lt;a href="@item.Url"&gt;@item.Text&lt;/a&gt;&lt;/li&gt;
  }
  </code></pre>