如何从多个 .aspx 文件引用相同的 CodeBehind 类(并能够预编译网站)?
我有一组 aspx 页面,每个页面都位于自己的目录中,并引用单个 aspx.cs 代码隐藏文件。
这以前工作得很好,因为我从未尝试过预编译该网站。 IIS 必须单独编译每个 aspx,将它们链接到 App_Code 的内容,但不能一次引用多个 aspx。
现在,我尝试使用 Web 部署项目预编译网站,但我不断收到有关在多个程序集中找到同一类的错误。
我不能只是将 aspx.cs 放在 App_Code 中并对其进行子类化,因为它在编译时无法找到 .aspx 页面上的控件。也许我可以在 .cs 中明确定义页面上的每个控件?但这能让它们正确连接吗?
关于如何从多个 .aspx 页面引用相同的 Page 类并能够预编译整个网站,还有其他想法吗?
编辑: 看起来 CodeFileBaseClass 可能就是我想要的。我将把 .cs 放入 App_Code,显式定义所有页面控件,然后在从其继承的 aspx 文件中将此类指定为 CodeFileBaseClass。想法?
编辑2: 我想出了一个解决方案,但由于我还不能将其标记为已接受,我仍然想看看其他人是否可以为这个问题提出一些其他解决方案。
I have a set of aspx pages that each live in their own directory and reference a single aspx.cs code behind file.
This has worked fine previously because I never tried to pre-compile the site. IIS must have individually compiled each aspx, linking them to the contents of App_Code but never referencing more than one aspx at a time.
Now that I'm trying to pre-compile the website using Web Deployment Projects I keep getting errors about the same class being found in multiple assemblies.
I can't just drop the aspx.cs in App_Code and subclass it because it wouldn't be able to find the controls on the .aspx pages when compiling. Maybe I could explicitly define every control on the page in the .cs? But would that allow them to be wired up correctly?
Any other ideas on how I can reference the same Page class from multiple .aspx pages and be able to pre-compile the entire website?
Edit:
It looks like CodeFileBaseClass may be what I want. I'll drop my .cs in App_Code, explicitly define all my pages controls, then specify this classs as CodeFileBaseClass in the aspx files that inherit from it. Thoughts?
Edit2:
I came up with a solution, but since I can't yet mark it as accepted, I'd still like to see if anyone else could come up with some other solutions for this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方案是:
FooPage
)FooPage
以显式声明它引用的所有服务器端控件。CodeFileBaseClass="Namespace.To.FooPage"
这允许 ASP.net 轻松连接所有服务器.aspx 中的侧面控件指向基类的受保护成员。
现在我的代码中每个类只有 1 个,并且可以预编译整个网站!
The solution was to:
FooPage
)FooPage
to explicitly declare all server side controls it references.CodeFileBaseClass="Namespace.To.FooPage"
in the .aspxThis allows ASP.net to easily wire up all the server side controls in the .aspx to the protected members of the base class.
Now I only have 1 of each class in my code and can pre-compile the entire site!
我认为您无法使用此框架执行预编译。
您可能想要构建一个执行所需功能的用户控件,然后可以根据需要将其包含在各个页面上。
I don't think you're going to be able to perform pre-compilation with this framework.
You may want to build a user control that performs the function you need and then you can have include it on the individual pages as needed.