2
votes

I'm trying to create an Html Helper, by creating a static class as follows:

public static string Crumbs(this HtmlHelper helper, params string[] args) where T : class
{
    // ... rest of code goes here.
}

And I'm invoking it like this:

<% Html.Crumbs(
    Html.ActionLink("Home", "Index", "Home"),
    Html.ActionLink("Lists", "Index", "User"),
    Html.Encode(Model.List.Name)); %>

However, the view does not compile, as I get the following compilation error:

CS1061: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'Crumbs' and no extension method 'Crumbs' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)

I don't get it. None of the documentation that I have mentions that you need to register the namespace of the static class anywhere. What am I doing wrong?

3

3 Answers

6
votes

You need to import the namespace of your extension in you view or in web.config.

In web.config:

<pages>
    <namespaces>
        <add namespace="MyExtensions.Namespace"/>

In your view:

<%@ Import Namespace="MyExtensions.Namespace" %>
1
votes

You need to register the namespace in web.config

<system.web>
    <pages>
        <namespaces>
            <add namespace="X.Y.Z"/>
        </namespaces>
    </pages>
</system.web>
1
votes

Make sure you put your helper in namespace (any) that is referenced in web.config or the page itself (Import Namespace).