简体   繁体   English

ASP.NET Web 用户控件库

[英]ASP.NET Web User Control Library

We have a bunch of user controls we would like to pull out of a web application and into a separate assembly/library, and I thought it would be as simple as creating a class library and pulling the ascx and ascx.cs files into the project, and compiling a DLL to be reused among our applications.我们有一堆用户控件,我们想从 Web 应用程序中提取出来并放入单独的程序集/库中,我认为这就像创建一个类库并将 ascx 和 ascx.cs 文件拉入项目一样简单,并编译一个 DLL 以便在我们的应用程序中重用。

This was not the case, however.然而,情况并非如此。

Our ultimate goal is to have a single distributable DLL (similar to how Telerik distributes their controls) that we can throw into any web application.我们的最终目标是拥有一个单独的可分发 DLL(类似于 Telerik 分发其控件的方式),我们可以将其放入任何 Web 应用程序中。 The steps here: Turning an .ascx User Control into a Redistributable Custom Control were very simple to follow, however this results in many files named controlname.ascx.guid.dll, which is not the desired result.此处的步骤:将 .ascx 用户控件转换为可再分发的自定义控件非常容易遵循,但是这会导致许多名为 controlname.ascx.guid.dll 的文件,这不是预期的结果。 I couldn't even get these to work anyways, since we have additional classes that need to be compiled into the assembly.我什至无法让这些工作无论如何,因为我们有额外的类需要编译到程序集中。

Has anyone successfully created a web user control library in .NET (we're using 3.5 here)?有没有人在 .NET 中成功创建了一个 web 用户控件库(我们在这里使用 3.5)? I can't seem to find a nice step-by-step guide.我似乎找不到一个很好的分步指南。

I realize this is an old topic, but if anyone is looking for a solution for creating reusable user control libraries, it turns out it's fairly simple.我意识到这是一个古老的话题,但如果有人正在寻找创建可重用用户控件库的解决方案,结果证明它相当简单。 Here are two good step-by-step guides along with source code:这里有两个很好的分步指南以及源代码:

From MSDN: Turning an .ascx User Control into a Redistributable Custom Control来自 MSDN:将 .ascx 用户控件转变为可再发行的自定义控件

From Code Project: Straight way to create ASP.NET user controls library来自代码项目:创建 ASP.NET 用户控件库的直接方法

The second link provides a solution to the multiple dlls created by the first link.第二个链接为第一个链接创建的多个 dll 提供了解决方案。

Edit- (2) Seems to be a dead link.编辑-(2)似乎是一个死链接。 Here's the new link这是新链接

https://www.codeproject.com/Articles/30247/Straight-way-to-create-ASP-NET-user-controls-libra https://www.codeproject.com/Articles/30247/Straight-way-to-create-ASP-NET-user-controls-libra

If you want to share controls among project, my experience has shown that the best way is to build custom asp.net server controls instead of usercontrols.如果您想在项目之间共享控件,我的经验表明最好的方法是构建自定义 asp.net 服务器控件而不是用户控件。 User controls are good for sharing within the same project, but not over multiple ones.用户控件适合在同一个项目中共享,但不适用于多个项目。

For this purpose I suggest you to build a set of custom server controls inside a class library and use that on all of your projects.为此,我建议您在类库中构建一组自定义服务器控件,并在您的所有项目中使用它。

This book does quite a good job at explaining the basics of creating server controls 这本书很好地解释了创建服务器控件的基础知识

Edit:编辑:
I'm currently developing a .net web server control library.我目前正在开发一个 .net Web 服务器控件库。 I actually didn't follow any step-by-step guide.我实际上没有遵循任何分步指南。 I mostly considered using the book I mentioned above and the MSDN library + Reflector, which is a great tool for inspecting existing MS server controls and learning from them.我主要考虑使用我上面提到的这本书和 MSDN library + Reflector,这是一个很好的工具,用于检查现有的 MS 服务器控件并从中学习。

我找到了教程创建和使用用户控件库,但它看起来有点像黑客,因为它依赖于构建后命令行事件将用户控件从一个项目复制到另一个项目。

Somewhat late, I admit.有点晚了,我承认。

To create a re-usable library of user controls;创建可重用的用户控件库; create a new Web Application Project, delete all the scaffolding, add a (number of) user control(s).创建一个新的 Web 应用程序项目,删除所有脚手架,添加(多个)用户控件。 Create a Web Deployment Project from the Web Application Project, in the WDP properties choose the option to Merge all control output and assign a name for the library and ensure that Allow this website to be updatable is NOT checked.从 Web 应用程序项目创建一个 Web 部署项目,在 WDP 属性中选择选项合并所有控件输出并为库分配一个名称,并确保未选中允许此网站可更新。

Build the WDP and use Reflector to examine the generated library;构建 WDP 并使用 Reflector 检查生成的库; you'll see that it contains an ASP namespace and the types you carefully crafted have been renamed ie usercontrol_ascx.您将看到它包含一个 ASP 命名空间,并且您精心制作的类型已被重命名,即 usercontrol_ascx。 In your target website(s) add references to BOTH the output dlls from your WDP, add a system.web/pages/controls node to web.config using the namespace ASP and the name of the assembly that you defined in the WDP.在您的目标网站中,添加对 WDP 输出 dll 的引用,使用命名空间 ASP 和您在 WDP 中定义的程序集名称将 system.web/pages/controls 节点添加到 web.config。

Now when you use the library in a page (for example) you must use the alias that you defined in web.config and the typename as seen in Reflector ie现在,当您在页面中使用库时(例如),您必须使用您在 web.config 中定义的别名和在 Reflector 中看到的类型名,即

<ucl:usercontrol_ascx ... />

I found it useful to add a dependancy for the website(s) on the WDP so that the WDP is built before the websites;我发现在 WDP 上为网站添加依赖项很有用,以便 WDP 在网站之前构建; now I can change the user controls in the WAP without having to remember to build the WAP before building the website(s).现在我可以更改 WAP 中的用户控件,而不必记住在构建网站之前构建 WAP。

I hope that someone finds this useful as it cost me a few grey hairs getting to this stage and still have VS do its 'automagically' thing.我希望有人觉得这很有用,因为我花了一些白发才到这个阶段并且仍然让 VS 做它“自动”的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM