[英]Accessing a Class that is NOT declared in App_Code in ASP.NET
I sometimes define Business Logic classes to "help" my ASPX code-behind classes. 有时,我定义业务逻辑类来“帮助”我的ASPX代码隐藏类。 It makes the most sense to me to include them both in the code-behind file since they work together. 对我来说,将它们都包含在代码隐藏文件中是最有意义的,因为它们可以一起工作。 However, I'd occasionally like to access the Business Logic classes from higher level classes defined in App_Code but they aren't automatically accessible outside of the file. 但是,我偶尔希望从App_Code中定义的更高级别的类访问Business Logic类,但是不能在文件外部自动访问它们。
Thus, the question: it is easy to access classes defined in App_Code but how do I access classes defined elsewhere? 因此,出现了一个问题:访问App_Code中定义的类很容易,但是如何访问其他地方定义的类呢?
UPDATE: One other thing, the ASPX page class and the App_Code class are in the same namespace - that isn't the issue. 更新:另一件事,ASPX页面类和App_Code类在同一个命名空间中-这不是问题。
NOTE: I have taken the advice of those who have responded (thanks guys) and am refactoring to make class access easier. 注意:我已听取了那些已做出答复(感谢伙计们)并正在进行重构的人员的建议,以简化班级访问。 However, I don't think the question is actually answered yet (in the case of an ASP.NET Website project). 但是,我认为问题尚未真正得到解决(以ASP.NET网站项目为例)。 I don't need the answer any more but, if someone could clarify what makes classes visible when they are outside of App_Code, it may well help someone else (or even me, down the road). 我不再需要答案了,但是,如果有人可以澄清是什么使类在App_Code之外时可见,那么它很可能会帮助其他人(甚至我,在路上)。
Make sure you place your classes in a sensible namespace. 确保将类放置在合理的名称空间中。
Place 'using' keyword in code behind files you would like to access them. 在代码中将“ using”关键字放在要访问它们的文件后面。
Or <%@ import if you are using them in inline code. 或<%@ import(如果您在内联代码中使用它们)。
Put the dll that contains your classes in the /bin folder. 将包含您的类的dll放在/ bin文件夹中。
TBH I prefer to keep the separate library project in the same solution and have project reference in the Web probject. TBH我更喜欢将单独的库项目保留在相同的解决方案中,并在Web probject中具有项目引用。 VS handles building and placing the dll for you. VS为您处理构建和放置dll的过程。
Some time while you create or add any file it has been create as the content file while it has to be compile file. 在创建或添加任何文件的某个时间,它已经被创建为内容文件,而必须是编译文件。
Please follow the process how to resolve this issue. 请按照以下过程解决此问题。
1) Right click on your class file in App_Code folder. 1)右键单击App_Code文件夹中的类文件。 2) Click on properties. 2)点击属性。 3) Change Build Action from "" to Compile 4) Rebuild your Application. 3)将“构建操作”从“”更改为“编译”。4)重建应用程序。
Tushar Tyagi 塔沙尔·柳吉
I assume you mean you are defining a separate class inside the codebehind .cs file? 我假设您的意思是您是在.cs文件背后的代码中定义一个单独的类? What access modifiers are you giving them? 您要给他们哪些访问修饰符?
As above though, I'd generally have a separate project in the solution for this kind of thing (usually in a different namespace like MyApp.Web and MyApp.), and just reference it from there. 如上所述,尽管如此,我在解决方案中通常会有一个单独的项目(通常在MyApp.Web和MyApp。等不同的命名空间中),然后从那里引用它。
You can also just create a standard folder in your project to access classes, and move them there, but you have to do some additional things to make it accessible to your project: 您也可以只在项目中创建一个标准文件夹来访问类,然后将它们移到那里,但是您还需要做一些其他事情以使项目可以访问它:
You can not access a class from another class in same code behind file as .net dsnt support multiple inheritance. 您不能从.net dsnt支持多重继承的文件后面的同一代码中的另一个类访问另一个类。 but you can create you business logic class as inner class in main class and make its all functions public so that they can be call in main class. 但是您可以在主类中将业务逻辑类创建为内部类,并将其所有功能公开,以便可以在主类中调用它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.