如何在自定义HtmlHelper扩展上修复intellisense?

时间:2011-11-17 21:30:14

标签: vb.net visual-studio-2010 razor intellisense html-helper

将VB.NET与MVC3和Razor一起使用。

我有一个针对 htmlhelper 的自定义扩展程序的测试页。 Visual Studio告诉我视图不正常:

index.vbhtml(view)

的代码
<h2>@ViewData("Message")</h2>

@Html.CustomLink("text 321312", 123)

错误:CustomLink不是System.Web.Mvc.HtmlHelper(..)的成员

LinkExtension.vb的

代码(扩展名)

Imports System.Runtime.CompilerServices
Imports System.Web.Mvc

Public Module htmlHelperExtensions
    <Extension()> _
    Public Function CustomLink(htmlHelper As HtmlHelper, linkText As String, uuid As Short) As MvcHtmlString
        Return MvcHtmlString.Create(String.Format("<a href="#{1}">{0}</a>", linkText, uuid )
    End Function
End Module

web.config(添加名称空间)

我在一般的web.config和view web.config中添加了对librairy的引用 (我也尝试将它添加到控制器中。)

<system.web>
  <pages>
    <namespaces>
      [...]
      <add namespace="linkExtension" />
    </namespaces>
  </pages>
</system.web>

页面正常运行并根据需要构建html 。但VS一直告诉我代码不正确( CustomLink不是'System.Web.HtmlHelper(Of Object)'的成员)!有什么想法吗?

1 个答案:

答案 0 :(得分:1)

尝试在添加命名空间时指定项目:

<add namespace="MyProject.linkExtension" />

如果您不确定它是什么,请找到htmlHelperExtensions所在的项目,在解决方案资源管理器中右键单击它,选择属性,然后在应用程序上标签使用“Root namespace”标签下的值。

此外,您的代码未显示扩展程序文件中定义的Namespace linkExtension的使用情况,因此如果上述操作无效,请尝试将其添加到您的模块中:

Namespace linkExtension
    Public Module htmlHelperExtensions
        <Extension()> _
        Public Function CustomLink(htmlHelper As HtmlHelper, linkText As String, uuid As Short) As MvcHtmlString
            Return MvcHtmlString.Create(String.Format("<a href="#{1}">{0}</a>", linkText, uuid )
        End Function
    End Module
End Namespace

如果这没有用,那么你在运行什么版本的MVC?有人使用VB.NET的similar issue was reported。在那个人的情况下,他们将所有项目升级到MVC 3,或者因为他们使用MVC 2而卸载了MVC 3.它显然在安装SP1和Azure Tools时也存在一些干扰。