为什么我的类无法从 ASP.NET 内联代码中获得?

发布于 2024-08-14 12:55:15 字数 531 浏览 2 评论 0原文

我有一个用 VS 2008 编写的 ASP.NET (C#) 应用程序,它使用 CodeBehind 文件和母版页。同一命名空间中还有其他类和扩展方法。现在,在某些页面上,我的类和方法未从内联代码中定义,例如 <%=MyClass.MyMethod().ExtensionMethod()%>。我可以写 <%=MyNamespace.MyClass.MyMethod()%>,但是 1. 它不必要地更长,2. 那么我无法使用扩展方法。在大多数页面上,它可以工作,但在某些页面上则不能。

我可以盲目地(没有 IntelliSense)输入代码,它会编译得很好,但是当访问页面时,它会抛出一些 HttpCompiler 异常,告诉我类/方法未定义。它建议我添加“使用”或参考。但是我应该如何在代码视图中的 .aspx 页面上插入“using [namespace]”?这仅适用于 C# 代码,不适用于 HTML 代码。我绝对不需要引用,因为它们都在同一个项目中,甚至在同一个命名空间中。

有谁知道为什么这有时不起作用?到底是什么原因,我什至不知道从哪里开始寻找问题,更不用说解决方案了......

I have an ASP.NET (C#) application, written with VS 2008, that uses CodeBehind files and master pages. There are also other classes and extension methods all in the same namespace. Now on some pages my classes and methods are undefined from inline code, like <%=MyClass.MyMethod().ExtensionMethod()%>. I can write <%=MyNamespace.MyClass.MyMethod()%>, but 1. it's unnecessarily longer and 2. I cannot use extension methods then. On most pages, it works, but on some it doesn't.

I can blindly (without IntelliSense) type in the code though and it will compile fine, but when accessing the page, it throws some HttpCompiler exception telling me that the class/method is undefined. It suggests me to add a "using" or a reference. But how am I supposed to insert a "using [namespace]" on a .aspx page in the code view? This is only available in C# code, not in HTML code. And I definitely don't need a reference because it's all in the same project, even in the same namespace.

Does anybody know why this isn't working sometimes? What's the reason at all, I don't even know where to start looking for the problem, not to mention a solution...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无畏 2024-08-21 12:55:15
<%@ Import Namespace="Namespace.Containing.ExtensionMethod" %>

或者,如果您希望它在所有页面上全局应用,您可以使用 web.config:

<pages>
  <namespaces>
    <add namespace="Namespace.Containing.ExtensionMethod"/>
  </namespaces>
</pages>
<%@ Import Namespace="Namespace.Containing.ExtensionMethod" %>

Or if you want it to apply globally on all pages you could use web.config:

<pages>
  <namespaces>
    <add namespace="Namespace.Containing.ExtensionMethod"/>
  </namespaces>
</pages>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文