Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC view can't find my extension method

I've created an extension method:

namespace MyComp.Web.MVC.Html
{
    public static class LinkExtensions
    {
        public static MvcHtmlString ActionImageLink(this HtmlHelper htmlHelper, string linkText, string imageSource, string actionName)
        {
            ...
        }
    }
}

I've referenced the assembly from my mvc app, and I've tried importing the namespace in my view:

<%@ Import Namespace="MyComp.Web.Mvc.Html" %>

and I've also added it to the web config file:

<pages>
    <controls>
        ...
    </controls>
    <namespaces>
        <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.Linq"/>
        <add namespace="System.Collections.Generic"/>
        <add namespace="MyComp.Web.Mvc.Html"/>
    </namespaces> 
</pages>

In My view if I try to access Html.ActionImageLink I get an error saying that System.Web.Mvc.HtmlHelper does not contain a definition for ActionImageLink accepting a first argument type of System.Web.Mvc.HtmlHelper. I don't see any of the ActionLink extension methods for System.Web.Mvc.HtmlHelper, only for System.Web.Mvc.HtmlHelper, so how does it work for the .net framework, and not for me?

like image 820
Jeremy Avatar asked Jan 21 '10 17:01

Jeremy


People also ask

What is the extension of view in MVC?

Views in MVC refer to either . cshtml files in C# or . vbhtml files in Visual Basic.

What is extension method in .NET core?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.


2 Answers

Notice the difference in the case of your namespace when declaring and when importing.

namespace MyComp.Web.MVC.Html
{
} 

<%@ Import Namespace="MyComp.Web.Mvc.Html" %>
<add namespace="MyComp.Web.Mvc.Html"/>

Namespaces are case-sensitive!

like image 80
Anthony Serdyukov Avatar answered Sep 28 '22 08:09

Anthony Serdyukov


You must add the namespace in the web.config but in the one inside the Views Folder

like image 27
LastCyborg Avatar answered Sep 28 '22 07:09

LastCyborg