为什么不能从ASP.NET在线code我的班? [英] Why are my classes not available from ASP.NET inline code?

查看:130
本文介绍了为什么不能从ASP.NET在线code我的班?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET(C#)应用程序,在VS 2008编写的,使用codeBehind文件和母版页。还有其他的类和扩展方法都在同一个命名空间。现在,在一些网页上我的类和方法来自行内code不确定的,就像<%= MyClass.MyMethod()ExtensionMethod()%>。我可以写<%= MyNamespace.MyClass.MyMethod()%>,但1这是不必要的时间更长,2。我不能使用扩展方法即可。在大多数页面,它的工作原理,但有些事实并非如此。

我可以盲目地(没有智能感知)在code型,虽然,它会编译罚款,但在访问页面时,它会抛出一些异常HttpCompiler告诉我,类/方法是不确定的。这表明我添加一个使用或引用。可是我怎么插入一个使用[命名空间]在code视图的.aspx页面上?这仅仅是在C#code可用的,而不是在HTML code。而且我绝对不需要引用,因为这一切都在同一个项目中,甚至在同一个命名空间。

是否有人知道这是为什么有时不工作?有什么道理可言,我甚至不知道从哪里开始查找问题,更不要说解决方案...


解决方案

 <%@导入命名空间=Namespace.Containing.ExtensionMethod%GT;

或者,如果你想让它你可以使用web.config中的所有页面全局应用:

 <网页和GT;
  <&命名空间GT;
    <添加命名空间=Namespace.Containing.ExtensionMethod/>
  < /命名空间>
< /页>

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

解决方案

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

这篇关于为什么不能从ASP.NET在线code我的班?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆