简体   繁体   English

如何从后面的代码调用App_Code文件夹中的类?

[英]How to call a class in the App_Code folder from code behind?

I created a class in ASP.NET C# which is located in the App_Code folder. 我在位于App_Code文件夹中的ASP.NET C#中创建了一个类。 Now I want to call this class from my code behind from one of my .aspx pages. 现在,我想从我的.aspx页之一的代码中调用此类。 How can I do this? 我怎样才能做到这一点?

Any help will be appreciate it. 任何帮助将不胜感激。

I'm assuming that you can't see the App_Code class from your code-behind, right? 我假设您无法从代码隐藏区看到App_Code类,对吗? Go to the solution explorer and in the properties of the class, change the Build Action to Compile . 转到解决方案资源管理器,然后在类的属性中将Build Action更改为Compile After making this change, you should be able to access the class in your code-behind. 进行此更改之后,您应该可以在代码隐藏中访问该类。

在App_Code文件夹中的类文件中,只需将属性“ Build Action”更改为Compile

生成启用Intellisense的项目后,请首先键入该类的名称空间,或在名称空间中添加using语句。

You can access your code if you've put it in the App_Code folder. 如果将代码放在App_Code文件夹中,则可以访问它。 Code in there is compiled dynamically at run time, and is available for you to use anywhere, so long as your classes are public. 其中的代码在运行时会动态编译,只要您的类是公共的,您就可以在任何地方使用它。 You want to make sure you get the namespace correct. 您要确保正确获得名称空间。 Suggest something like this: 建议这样的事情:

namespace MyNamespace.App_Code {
    public class MyClass {

Then, in your code reference this using: 然后,在您的代码中使用以下代码进行引用:

MyNamespace.App_Code.MyClass x = new MyNamespace.App_Code.MyClass();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM