views:

655

answers:

5

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?

A: 

One of the reasons may be you are returning a MvcHtmlString and not a string. Include the namespace for class MvcHtmlString. See if it helps.

San
The existing ActionLink extension methods return MvcHtmlString too though.
Jeremy
The return value of the function has nothing to do with it.
Baddie
I tried the above code from Jeremy, it works fine for me. Its weird, that its not working for Jeremy.
San
A: 

Try shutting down Visual Studio and opening your Solution again. When things start acting weird, some times this helps.

This Answer stinks.

hunter
A: 
  1. Does the VS intellisense autocompletes your extension method? Does it autocompletes standard MVC helpers methods? If not then the view complilation error occured. Make sure you have the proper "Inherits" attribute value in Page tag at the beginning of the view. If you use strongly typed views make sure the "strong type" exists and compiles.

  2. Do you define the extension method in the same project where the view is defined? If not you have to add the reference in the mvc project. Finally check if the assembly with the extension method (MyComp.Web.Mvc.Html.dll?) is in the Bin folder of the application

  3. Try to add the namespace declaration to the pages/namespaces section of the web.config file placed in your Views folder in MVC project (not the main project web.config file).

PanJanek
+4  A: 

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!

Anton
Arg! I did know they were case sensitive, but I didn't catch the typo. But then again, neither did anyone else but you! HA!
Jeremy
good catch, missed that
hunter
A: 

i see this thread is a bit old however for those it might help: your imported namespaces should include the name of your static class ie:

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

in web config :

<add namespace="MyComp.Html.LinkExtensions"/>

instead of <add namespace="MyComp.Html"/>

winy101