编译/嵌入 ASCX 模板化 UserControls 以在多个 Web 应用程序中重用

发布于 2024-07-23 23:29:25 字数 1028 浏览 13 评论 0原文

我在这里遇到了一个真正令人头疼的问题……它似乎是 ASP.NET 中最令人沮丧的主题之一。

我有一个程序集,它实现了很多自定义 Linq 内容,其核心是零 Web 功能。 我有一个附加程序集,可以使用特定于网络的行为扩展此程序集。

Web 特定行为附带在 ASCX 模板化用户控件内标记的几个用户控件。

我无法对该程序集进行良好的处理,以便可以轻松地重新部署以在其他应用程序中使用。 让我回顾一下到目前为止我所尝试过的操作:

  1. 使用构建事件将 ASCX 文件复制到使用的 Web 应用程序; 远非理想,部署噩梦
    • 实现了自定义 VirtualPathProvider 并将 ASCX 模板作为嵌入式资源嵌入到程序集中。 不幸的是,当在消费应用程序中使用 Register 指令时,它会将设计器声明创建为 UserControl,其中我需要实际控件类型的声明; (通常)不可预见且不可取的
    • 创建了一个 Web 部署项目来编译 UserControl,但编译后的用户控件随后成为另一个程序集的一部分,并且不再从我的 Web 程序集中的类定义派生 -程序集需要实例化它们,依赖于请求上下文

因此,第 1 点只是废话,第 2 点没有给我我想要的类型支持,而第 3 点我想我将产生一个合理的解决方案:

  • 将所有非控制类集中到 App_Code 中> 文件夹,准备一个工厂类,它将使用反射构造所需控制类型的对象,并期望所反射的类型将出现在部署输出中(希望通过 ClassName 的存在来保证) Control 指令中的属性)。

还有另一种选择,将 ASCX 控件重写为自定义控件,但目前没有资源来考虑它,而且我们没有这方面的专业知识,而且它们作为用户控件工作得很好。

我是否遗漏了一些明显的东西,一些可能更简单的东西,或者这只是故意困难的? 在我浏览该主题的过程中,我读到过有关 ASP.NET 编译过程的设计非常不幸的故事。

I'm onto a real head scratcher here ... and it appears to be one of the more frustrating topics of ASP.NET.

I've got an assembly that implements a-lot of custom Linq stuff, which at it's core has zero web functionality. I have an additional assembly that extends this assembly with web specific behaviour.

The web specific behaviour comes with a couple of user controls marked up inside ASCX templated UserControls.

I'm having trouble putting a nice finish on this assembly so that it is simple to redeploy for use in other applications. Let me run through what I've tried so far:

  1. Copied the ASCX files to the consuming web application using build events; far from ideal and quite a deployment nightmare.
    • Implemented a custom VirtualPathProvider and embedded the ASCX templates within the assembly as embedded resources. Unfortunately when using the Register directive in the consuming application it creates the designer declaration as a UserControl, where I would require a declaration of the actual control type; unforeseen (typically) and undesirable.
    • Created a Web Deployment Project to compile the UserControls, but the compiled user controls then become part of another assembly, and no longer descend from the class definitions in my web assembly--the assembly needs to instantiate them dependent on the request context.

So number 1 is just crap, number 2 doesn't give me the type support I desire and number 3 I think I'm about to produce a reasonable solution with:

  • Lump all non-control classes into the App_Code folder, prepare a factory class that will construct an object of the desired control type using reflection and the expectation that the type being reflected will be present in the deployment output (hopefully guaranteed by the presence of the ClassName attribute in the Control directive).

Theres also always the other option of rewriting the ASCX controls into custom controls, but just don't have the resources to consider it at the moment, and we've got no expertise in doing that, and they work fine as UserControls.

Am I missing something obvious, something maybe much simpler, or is this just purposefully difficult? I've read stories of the ASP.NET compilation process being very unfortunate in it's design on my travels across this topic.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初心未许 2024-07-30 23:29:25

好吧,我想我已经做到了......通过注意我最后一种方法中的一些恼人的陷阱,在使用 Web 部署项目在 Web 应用程序项目中编译 ASCX 用户控件时,我建议执行以下操作:

  1. 避免放置类在 App_Code 中,除非它们是独立类或辅助类,否则 ASP.NET 会将其视为 speshul 文件夹,我不明白其含义,随之而来的是混乱、混乱和混乱。 不过,此文件夹中的代码确实会在 Web 部署项目中获得输出。
    • 密切注意您的程序集名称、其根命名空间和部署输出程序集名称 - 如果在 aspnet_merge访问被拒绝错误> 过程。
    • 最终您很可能会部署 2 个程序集,我尝试只创建一个程序集,但部署输出仍然指向源程序集中的类型定义。 如果您的 Web 应用程序项目中没有任何其他类型,那么这不是问题 - 我有,所以这对我来说是一个问题。 就我而言,我的最终输出是:
    • ..Web.DLL - 已编译的 Web 应用程序程序集(包含 ASCX 模板)
    • ..Web.UI.DLL - ASP.NET 编译的 UserControl 程序集,由 Web 部署项目创建
    • 经常清理,并检查 Web 应用程序项目的 binobj 路径是否已清除以前在您可能尚未最终确定命名空间或程序集命名时构建的垃圾内容方案——Web 部署项目将非常热衷于包含这些方案,从而造成混乱。
    • 检查导入的命名空间,ASP.NET 编译器喜欢引用 ASCX 模板中的 Import 指令,并且它还会考虑 web.config 中存在的导入命名空间的 元素,如果在部署过程中出现未知定义,请进行调整。

要有耐心,这很棘手! 但最后你确实得到了一些不错的可重新分发的用户控件!

唷!

Well I think I've done it ... by being mindful of a few of a few annoying pitfalls with my last approach, I recommend the following when compiling ASCX user controls in a Web Application Project using a Web Deployment Project:

  1. Avoid putting classes in App_Code unless they're standalone or helper classes, ASP.NET treats this as a speshul folder, the meaning of which is lost on me, mayhem, confusion and chaos follows. Code in this folder does get output in the Web Deployment Project, though.
    • Pay close attention to your assembly names, their root namespaces and deployment output assembly name- you'll get access is denied errors if you have any naming conflicts during the aspnet_merge process.
    • Ultimately you'll most likely end up deploying 2 assemblies, I tried to create only one but the deployment output was still pointing to type definitions in the source assembly. This isn't a problem if you don't have any other types in your Web Application Project--I have so it was a problem for me. In my case, my final output was:
    • <Organisation>.<TechnologyName>.Web.DLL - Compiled Web Application Assembly (containing the ASCX templates)
    • <Organisation>.<TechnologyName>.Web.UI.DLL - ASP.NET Compiled UserControl assembly, created by Web Deployment Project
    • Clean often, and check that the Web Application Project's bin and obj paths are cleared of any previous junk built when you perhaps hadn't finalised your namespace or assembly naming scheme--the Web Deployment Project will be quite keen to include these, causing a fine mess.
    • Check your imported namespaces, the ASP.NET compiler likes to refer to the Import directive in the ASCX template, and it also considers imported namespaces present in web.config's <configuration><system.web><pages><namespaces> element, tweak if you get unknown definitions appearing during the deployment process.

Have some patience spare, it's quite tricky! But you do get some nice re distributable UserControls at the end of it!

Phew!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文