简体   繁体   English

访问ASP.NET中App_Code中未声明的类

[英]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)重建应用程序。

This will work definitely 这肯定会工作

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: 您也可以只在项目中创建一个标准文件夹来访问类,然后将它们移到那里,但是您还需要做一些其他事情以使项目可以访问它:

  • Ensure you don't name the folder any of the reserved terms (ie call it "AppCode", instead of "App_Code") if you are running into issues with the "App_Code" folder, after going through the rest of these bullets. 在浏览完所有其余项目符号后,如果遇到“ App_Code”文件夹问题,请确保未为该文件夹命名任何保留条款(即,将其命名为“ AppCode”,而不是“ App_Code”)。
  • The classes should all have the same namespace as your code-behind. 这些类都应具有与您的代码隐藏相同的名称空间。
  • Ensure the classes are made public, with public methods, if they are being called from other classes. 如果从其他类中调用它们,请确保使用公共方法将这些类公开。
  • Include "using MyClass;" 包括“使用MyClass;” statements at the top of the code-behind/class files. 代码隐藏/类文件顶部的语句。
  • Make sure the class' Build Action property is set to Compile. 确保类的“构建操作”属性设置为“编译”。

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.

相关问题 ASP.NET中的类App_Code中的局部类 - Class in ASP.NET Partial Class in App_Code app_code文件夹中的asp.net使用类 - asp.net use class in app_code folder 为什么abc.aspx.cs的类在asp.net的App_code文件夹的类中不可见 - why class for abc.aspx.cs is not visible in class of App_code folder in asp.net 无法从ASP.Net网站项目中的代码访问App_Code中的类 - Unable to access class in App_Code from code behind in ASP.Net Website Project 如何访问不在ASP.NET中的app_code中的全局类中的变量 - How to access variable in a global class not in app_code in asp.net 尝试从ASP.NET中的App_Code文件夹中的另一个类派生时键入冲突 - Type conflict when trying to derive from another class in the App_Code folder in ASP.NET 无法从asp.net网站项目中的app_code文件夹访问类 - Can't access class from app_code folder in asp.net website project 如何从app_code中的类设置母版页中的属性? 与asp.net C# - How to set property in master page from class in app_code? with asp.net c# 在ASP.NET app_code文件夹中调用移动检测类方法 - Call mobile detection class method in ASP.NET app_code folder 在dll中调用Activator.CreateInstance来实例化位于Asp.Net App_Code中的实例类 - Call Activator.CreateInstance in a dll to intance class that reside inside Asp.Net App_Code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM