【发布时间】:2011-10-31 13:56:30
【问题描述】:
我的代码可以在 .NET 3.5 应用程序中运行,但当我在 .NET 4.0 中以相同方式设置它时,它却无法运行。当我尝试扩展 HtmlHelper 时,问题就来了。在 .NET 4.0 MVC3 中,如果我有一个如下所示的页面:
<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of IEnumerable (Of MvcApplication2.MyModel))" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%=Html.MyTest%>
</asp:Content>
还有这样的扩展:
Imports System.Runtime.CompilerServices
Module Extensions
<Extension()>
Public Function MyTest(htmlhelper As System.Web.Mvc.HtmlHelper) As String
Return Now.ToLongTimeString
End Function
End Module
它不会编译,因为“Html”不再像过去那样属于System.Web.Mvc.HtmlHelper 类型,而是现在属于System.Web.Mvc.HtmlHelper(Of IEnumerable (Of MvcApplication2.MyModel)) 类型。 <%=Html.MyTest%> 上的错误信息是:
'MyTest' is not a member of 'System.Web.Mvc.HtmlHelper(Of System.Collections.Generic.IEnumerable(Of MvcApplication2.MyModel))'.
如何在此配置中扩展 HtmlHelper?我是否在设置中遗漏了导致“Html”与过去不同类型的内容?我没有在网上找到任何关于行为改变的信息,这让我相信我在某个地方做错了什么。
更新:
关于继承自 HtmlHelper 的 HtmlHelper(Of TModel),为什么这段代码不起作用?
Partial Public Class HtmlHelper
Public Function MyOtherTest() As String
Return Now.ToLongTimeString
End Function
End Class
引用 <%=Html.MyOtherTest%> 会产生相同的错误消息。
【问题讨论】:
-
你找出问题所在了吗?
-
我怀疑我的 web.config 有问题。我不确定到底是什么,因为我重新转换了我的项目,然后有条不紊地检查了 web.config 以确保我有正确的设置,这一次它工作了(可能是 pages\namespaces 部分)。不过,您的回复使我没有走上完全不同的道路,因此非常感谢。
标签: asp.net-mvc-3 .net-4.0 html-helper