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?
Views in MVC refer to either . cshtml files in C# or . vbhtml files in Visual Basic.
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.
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!
You must add the namespace in the web.config but in the one inside the Views Folder
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With