rhughes
rhughes

Reputation: 9583

MVC 4 and Extension Methods with Razor

I have created an extension method and have included its namespace in my web.config 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.

The ASP .NET error message I am getting is:

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?)

Below is the respective code.

Web.config:

<system.web>
    <httpRuntime targetFramework="4.5" />
    <compilation debug="true" targetFramework="4.5" />
    <pages>
        <namespaces>
            <add namespace="System.Web" />
            <add namespace="System.Web.Helpers" />
            <add namespace="System.Web.Mvc" />
            <add namespace="System.Web.Mvc.Ajax" />
            <add namespace="System.Web.Mvc.Html" />
            <add namespace="System.Web.Routing" />
            <add namespace="System.Web.WebPages" />
            <add namespace="MyMainSite2.Library.Extensions" />
        </namespaces>
    </pages>
</system.web>

Extension method 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("/") && absolutePath.StartsWith("/"))
                absolutePath = absolutePath.Remove(0, 1);

            bool match = absolutePath == url;

            return match;
        }

        #endregion
    }
}

Razor 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
    {
        @:<li class="current">
    }
    else
    {
        @:<li>
    }

    @:<a href="@item.Url">@item.Text</a></li>
}

Upvotes: 15

Views: 19255

Answers (2)

Shafiq Jetha
Shafiq Jetha

Reputation: 1556

I'd just like to post that I have been having this issue for a good few months now, and I followed the guide on the ASP.NET site, line by line, and I was able to get Intellisense back for Razor files.

Here is the link: http://www.asp.net/mvc/overview/releases/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2

I hope this helps prevent other people losing their hair as well.

Upvotes: 0

rhughes
rhughes

Reputation: 9583

The answer was given by petro.sidlovskyy.

I was adding the namespace to the main Web.config rather than the view's Web.config.

When I added the namespace to the Web.config in the Views folder, the namespace was recognised by the view, and thus the problem was solved.

Upvotes: 22

Related Questions