代码之家  ›  专栏  ›  技术社区  ›  Jeremy

MVC视图找不到我的扩展方法

  •  11
  • Jeremy  · 技术社区  · 15 年前

    我创建了一个扩展方法:

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

    我从MVC应用程序中引用了程序集,并尝试在视图中导入命名空间:

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

    我还将其添加到web配置文件中:

    <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>
    

    在我看来,如果我试图访问html.actionImageLink,我会得到一个错误,说system.web.mvc.htmlhelper不包含actionImageLink的定义,它接受第一个参数类型system.web.mvc.htmlhelper。我没有看到system.web.mvc.htmlhelper的任何actionlink扩展方法,只看到system.web.mvc.htmlhelper,那么它如何在.net框架中工作,而不是对我?

    6 回复  |  直到 11 年前
        1
  •  14
  •   Anthony Serdyukov    15 年前

    请注意声明和导入时命名空间大小写的不同。

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

    命名空间区分大小写!

        2
  •  6
  •   GEOCHET S.Lott    12 年前

    必须在 web.config配置 但在里面的那个 视图文件夹

        3
  •  4
  •   GEOCHET S.Lott    12 年前

    尝试关闭Visual Studio并再次打开解决方案。当事情开始变得奇怪时,有时这会有帮助。

        4
  •  1
  •   PanJanek    15 年前
    1. vs intellisense会自动完成扩展方法吗?它会自动完成标准的mvc helpers方法吗?如果不是,则发生视图编译错误。确保在视图开头的page标记中有适当的“inherits”属性值。如果使用强类型视图,请确保“强类型”存在并编译。

    2. 是否在定义视图的同一项目中定义扩展方法?如果没有,则必须在mvc项目中添加引用。最后用扩展方法检查程序集(mycomp.web.mvc.html.dll?)在应用程序的bin文件夹中

    3. 尝试将命名空间声明添加到mvc项目(而不是主项目web.config文件)中views文件夹中web.config文件的pages/namespaces部分。

        5
  •  1
  •   Shan    11 年前

    关闭并重新打开visual studio成功了!!

        6
  •  0
  •   San    15 年前

    原因之一可能是返回的是mvchtmlstring而不是string。 包括类mvchtmlstring的命名空间。看看是否有用。