App_Code 和 web.config
假设我在 App_Code 中有一个类 (MyClass.cs)(使用 ASP.NET 网站,而不是项目)。 没有为该类分配命名空间。
如何使该类在网站 .aspx 页面中有效? 我需要在 web.config 文件中放入什么以及我需要将其放在哪里?
我是<添加程序集标签还是<添加类型?
由于程序集标记需要版本、区域性和公钥,因此我不确定编译时这些值是什么。
我只需添加类型标签吗? 我应该把它放在 web.config 的什么地方?
编辑: 好的,我找到了部分答案。 我收到错误是因为“http://localhost/MyFolder”未在 IIS 中设置为“应用程序”。 但是,我有一堆文件夹,“http://localhost/MyFolder2,http://localhost/MyFolder3 等...
新问题:有什么方法可以不让 MyFolder 成为应用程序,但仍使其正确运行?我听说过“codesubdirectories”标签,这有用吗?我应该把它放在哪里?谢谢。
Let's say I have a class (MyClass.cs) in App_Code (using a ASP.NET web site, not project). There's no namespace assigned to the class.
How can I get that class to be valid in the web site .aspx pages? WHAT do I need to put in the web.config file and WHERE do I need to put it?
Do I <add assembly tag or do I <add type ??
Since the assembly tag requires version, culture, and public key, I'm not sure what those values are at compile time.
Do I just add a type tag? Where do I put it in the web.config?
EDIT:
Ok, I found part of my answer. I was getting the error because "http://localhost/MyFolder" was not set as an "application" in IIS. However, I have a BUNCH of folders, "http://localhost/MyFolder2, http://localhost/MyFolder3, etc...
New question: Is there any way to NOT have MyFolder be an application, and still make it run correctly? I've heard of a "codesubdirectories" tag, is that useful and where would I put it? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为你根本不需要做任何事情。 这就是网站“项目”的运作方式。 App_Code 中的任何内容都会被编译。
I don't think you have to do anything at all. That's how web site "projects" work. Anything in App_Code gets compiled.
检查一下以确保该类是公共的,也许?
Check to make sure the class is Public, maybe?
根据您的编辑,您是否尝试将 App_Code 文件夹移动到网站的根目录?
这可能会解决您的问题。
您还可以看看 Scott Guthrie 的提示/技巧:
尽管这确实使用了 Web 应用程序项目,而不是网站。
查看 CodeSubDirectories 配置元素 - 您可能可以使用它 - 它我猜需要在根 web.config 中定义。
另请注意,<程序集> 如果程序集是强命名的(因此具有这些值),则引用仅需要包含版本、区域性和公钥详细信息。
Going by your edit, have you tried moving the App_Code folder to the root of the site?
That may address your issue.
You could also take a look at Scott Guthrie's Tip/Trick:
Although this does use the Web Application projects, rather than web sites.
Looking at the CodeSubDirectories config element - you probably could use this - it would need to be defined in the root web.config I guess.
Also, note that the <assembly> references only need to contain version, culture and public key details if the assemblies are strongly named (and so have those values).
在解决方案资源管理器中检查代码文件的属性。
我遇到过一种情况,其中一个 .cs 代码文件为构建操作选择了“内容”。 App_Code 中的所有其他文件都可以正常编译,但这个不行。
将构建操作更改为“编译”,它开始按预期工作。
Check the properties of the code file in Solution Explorer.
I had a situation where one of the .cs code files had "Content" selected for the Build Action. All the other files in App_Code were compiling fine but not this one.
Changed the Build Action to "Compile" and it started working as expected.
在网站中,App_code 文件夹中不需要命名空间。 不需要进入 web.config 来引用 app_code 文件夹中的类。
In a web site, no name spaces are needed within your App_code folder. Nothing needs to go into your web.config to reference classes in your app_code folder.
这是首次引入 asp.net 2.0 网站时突然出现的动态编译的记录不足的方面之一。
不过,您的具体问题的实际答案需要您告诉我们更多有关您到底想对 App_Code 中的类执行什么操作的信息。 大多数时候,您不必担心命名空间或程序集名称即可在页面中使用这些类。 您只需使用类名,编译器就会计算出它并为您连接所有内容。
我遇到的最大例外是使用我放入 app_code 的 Web 控件时。 对于那些您需要在 aspx 页面中使用 @Register 指令...并且为此您需要程序集名称和命名空间。
app_code 中的文件被编译成名为“__code”的程序集(请注意,它有两个下划线,而不是一个)。 当您需要 Register 指令或 web.config 或其他内容的程序集名称时,您可以使用它。
但是,据我所知,您将无法在注册指令或某些需要命名空间的 web.config 设置中使用类,除非您已将该类显式包装在命名空间块中。
This is one of those poorly documented aspects of dynamic compilation that cropped up when asp.net 2.0 web sites were first introduced.
An actual answer for your specific question though will require that you tell us more about what exactly you are trying to do with the class from App_Code. Most of the time you just don't have to worry about the namespace or assembly name to use those classes in your pages. You just use the class name and the compiler will figure it out and hook everything up for you.
The biggest exceptions I've run into with this are when using web controls that I've put into app_code. For those you need a @Register directive in the aspx page... and for that you need an assembly name and a namespace.
The files in app_code are compiled into an assembly named "__code" (note that this has TWO underscores, not one). That's what you can use when you need the assembly name for Register directives or in web.config or what not.
But, as far as I know, you will not be unable to use a class in register directives or some web.config settings that require a namespace unless you have explicitly wrapped that class in a namespace block.