App_Code 之外的页面类将无法编译
我有一个网站,有如下 2 个文件:
- page.aspx
- page.aspx.cs
过去,我只需将新文件拖放到 Web 服务器上,IIS 就会自动编译这些文件,我就可以访问该页面,例如
http://www.website.com/page.aspx
...以及 中的相关功能.cs 文件中包含的页面类可以很好地工作。
现在我收到错误:“无法加载类型namespace.classname”,它引用了我的页面类。
现在,出于某种奇怪的原因,我必须将所有 .cs 文件,甚至页面类放入 app_code 文件夹中。
我网站上发生的所有变化是我重新组织了结构,这样我的页面不再位于 Web 根目录上,而是位于 http://.../newfolder/page.aspx。
由于某种原因,我的所有 page.aspx.cs 文件现在都必须位于 app_code 中。
有什么想法吗?
I have a website that has 2 files as follows:
- page.aspx
- page.aspx.cs
It used to be that I could just drop new files onto the web server and IIS would automatically compile the files and I could access the page e.g.
http://www.website.com/page.aspx
... and the associated functionality in the page class contained in the .cs file would work nicely.
Now I get the error: "Could not load type namespace.classname" which refers to my page class.
Now for some strange reason I have to put all my .cs files, even page classes into the app_code folder.
All that has changed on my website is that I reorganised the structure so that instead of my pages being on the web root they are now inside http://.../newfolder/page.aspx.
For some reason all my page.aspx.cs files now have to be in app_code.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您好像混淆了 Web 应用程序项目和网站。
您确定这些文件完全相同吗?也许一个 @Page 指令说 CodeBehind=Page.aspx.cs 而另一个说 CodeFile=Page.aspx.cs?
CodeBehind 需要项目编译,因此您不能只是放入新的 .cs 文件,您需要上传新编译的 DLL。 CodeFile 将允许动态编译。
当您的应用程序被访问时,App_Code 目录会被动态编译(在这两种情况下),因此当您将文件放在那里时,Inherit 指令具有有效的类型。一般来说,不要这样做。您希望 .cs 文件与 .aspx 文件一起使用。将 App_Code 用于与特定页面不关联的业务逻辑或实用程序类。
最后,这个新的子目录是否在 IIS 中设置为新应用程序?新目录中的 web.config 文件发生了什么变化?您运行的是相同版本的 ASP.NET 吗?检查“编译”标签。我不确定你可以做什么来导致这种情况,但我确信你可能会造成一些混乱。
Sounds like you are mixing up a Web Application Project and a Web Site.
Are you sure the files are exactly the same? Perhaps one @Page directive says CodeBehind=Page.aspx.cs and the other says CodeFile=Page.aspx.cs?
CodeBehind requires project compilation, so you cannot just drop in a new .cs file, you need to upload a new compiled DLL. CodeFile will allow dynamic compilation.
The App_Code directory is dynamically compiled (in both cases) when your app is accessed, so the Inherit directive has a valid type when you put the file there. In general, don't do this. You want the .cs file to go with the .aspx file. Use App_Code for business logic or utility classes that aren't associated with a particular page.
Finally, is this new subdirectory set up as a new app in IIS? What does the web.config file in your new directory change? Are you running the same version of ASP.NET? Check the "compilation" tag. I'm not sure what you could do there to cause this, but I'm sure you could cause some chaos.