Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
}
like image 358
rhughes Avatar asked Aug 30 '12 10:08

rhughes


People also ask

Does MVC use Razor?

Razor is one of the view engines supported in ASP.NET MVC. Razor allows you to write a mix of HTML and server-side code using C# or Visual Basic.

What are extension methods in MVC?

What is extension method? Extension methods in C# are methods applied to some existing class and they look like regular instance methods. This way we can "extend" existing classes we cannot change. Perhaps the best example of extension methods are HtmlHelper extensions used in ASP.NET MVC.

Is Cshtml a Razor?

All Razor files end with . cshtml. Most Razor files are intended to be browsable and contain a mixture of client-side and server-side code, which, when processed, results in HTML being sent to the browser. These pages are usually referred to as "content pages".

What is Razor in MVC why it is used?

Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML.


1 Answers

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.

like image 154
rhughes Avatar answered Oct 20 '22 21:10

rhughes