1

下面是我在名为 Presentation 的项目中的代码,我还有另一个名为 MyNamespace 和 MyNamespace2 的项目(在同一解决方案中),其中我有由 EntityFramework 生成的类。我将 MyNamespace 项目的引用添加到 Presentation 项目中。

1: <asp:Repeater ID="MyRepeater" runat="server" ItemType="MyNamespace.MyEntityClass" SelectMethod="SelectMethod">
2:     <ItemTemplate>
3:         <li><a href="<%# Item.Link %>">
4:             <%# Item.Text %>
5:         </a></li>
6:     </ItemTemplate>
7: </asp:Repeater>

现在,当我尝试在ItemType属性后的引号内使用 IntelliSense 时,我只能从 MyNamespace2 中获得类,但没有从 MyNamespace 中获得。此外,当我尝试运行此 Web 应用程序时,导航到包含上述代码的页面,即使构建成功,我也会收到以下错误:

Compiler Error Message: CS0246: The type or namespace name 'MyNamespace' could not be found (are you missing a using directive or an assembly reference?)

有趣的是,在设计器中,IntelliSense 使用Item's 属性,因此Item设计器实际上正确解析了 的类型。

现在这里出了什么问题?谢谢。

4

1 回答 1

2

我发现了我的问题。在 Presentation 中引用其他项目(MyNamespace)似乎是不够的。这样做让我可以在代码隐藏文件中使用 MyNamespace 中的类,但不能在 aspx 文件中使用。

我在我的 Web.Config 文件中添加了以下几行,以便在 aspx 页面中使用这些类:

<configuration>
....
  <system.web>
  ....
    <compilation debug="true" targetFramework="4.5">
      ....
      <assemblies>
        .....
        <add assembly="MyNamespace, Version=1.0.0.0, Culture=neutral, PublicKeyToke=null" />
      </assemblies>
    </compilation>
    ....
    <pages>
    ....
      <namespaces>
        ....
        <add namespace="MyNamespace" />
      </namespaces>
    </pages>
  </system.web>
</configuration>
于 2013-09-05T09:50:03.947 回答