将 web 应用程序项目 ascx 编译成 dll

Compile web application project ascx into dll(将 web 应用程序项目 ascx 编译成 dll)

本文介绍了将 web 应用程序项目 ascx 编译成 dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将 Web 应用程序项目 .ascx(用户控件)编译成 dll?

Is it possible to compile a web application project .ascx (user control) into a dll?

我想做以下事情:

  1. 在多个网站中使用相同的控件
  2. 将 css 和 .js 作为资源嵌入到控件中
  3. 能够更新超级容易.如果用户控件更新,我只想更新1个.dll

我已成功关注这篇文章,http://msdn.microsoft.com/en-us/library/aa479318.aspx.

I have successfully followed this article, http://msdn.microsoft.com/en-us/library/aa479318.aspx.

但是,它使用网站项目,我无法将 js css 作为资源嵌入到网站项目中.

However, that uses web site projects, and I cannot embed js css as resources into web site projects.

有什么想法吗?我是不是搞错了,我应该改变方法吗?

Any ideas? Am I going about this wrong, should I change the approach?

推荐答案

转换很容易,甚至可以完全自动化.它只需要更改您希望嵌入 ASCX 控件的 DLL 项目中的一些设置和基类.

Conversion is easy, and could even be fully automated. It simply requires changing a few settings and base classes in the DLL Project you want your ASCX controls embedded in.

1... 对于每个 UserControl,将 ASCX 文件的 Build Action(在 Properties 下)设置为Embedded Resource",并删除其关联的设计器文件.

1... For each UserControl, set the ASCX file's Build Action (under Properties) to "Embedded Resource", and delete its associated designer file.

2...保存项目.

3... 右键单击​​项目并选择卸载项目".

3... Right click the project and choose "Unload Project".

4... 再次右键单击它并选择编辑 *.csproj"选项.

4... Right click it again and choose the "Edit *.csproj" option.

更改如下所示的部分(星号代表您的班级名称):

Change sections that look like this (where the asterisk represents your class name):

<Compile Include="*.ascx.cs">
    <DependentUpon>*.ascx</DependentUpon>
    <SubType>ASPXCodeBehind</SubType>
</Compile>

看起来像这样

<Compile Include="*.ascx.cs" />

这将导致代码隐藏文件独立于 ASCX 文件进行编译.

That will cause the code-behind files to be compiled independently of the ASCX files.

5...保存更改,然后右键单击项目并选择重新加载项目".

5... Save changes, and right click the project and choose "Reload Project".

6... 打开所有*.ascx.cs"文件并让它们继承自以下自定义 UserControl 类,而不是 System.Web.UI.UserControl 类(您可以需要找到父类来完成这一步).

6... Open all your "*.ascx.cs" files and make them inherit from the following custom UserControl class, instead of the System.Web.UI.UserControl class (you may need to locate parent classes to complete this step).

public class UserControl : System.Web.UI.UserControl
{
    protected override void FrameworkInitialize()
    {
        base.FrameworkInitialize();
        string content = String.Empty;
        Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream( GetType().FullName + ".ascx" );
        using (StreamReader reader = new StreamReader(stream))
            content = reader.ReadToEnd();
        Control userControl = Page.ParseControl( content );
        this.Controls.Add( userControl );
    }
}

这个基类将负责加载和解析嵌入的 ASCX 文件.

This base class will take care of loading and parsing the embedded ASCX file.

7... 最后,您可能需要将 ASCX 文件放在子文件夹中,以便它们的资源名称(由文件夹路径自动确定)与其关联类的完整类型名称匹配(加上.ascx").假设您的根命名空间与您的项目名称匹配,则名为ProjectName.Namespace1.Namespace2.ClassName"的类将需要其 ASCX 文件位于子文件夹Namespace1Namespace2"中,因此它嵌入名称为ProjectName.Namespace1.Namespace2".类名.ascx".

7... Finally, you may need to place ASCX files in subfolders so that their resource names (automatically determined by folder path) match the full type name of their associated class (plus ".ascx"). Assuming your root namespace matches your project name, then a class named "ProjectName.Namespace1.Namespace2.ClassName" will need its ASCX file in a subfolder "Namespace1Namespace2", so it gets embedded with the name "ProjectName.Namespace1.Namespace2.ClassName.ascx".

就是这样!编译 DLL 并将其包含在另一个项目中后,您可以使用new"运算符实例化用户控件的实例,就像任何其他类一样.与往常一样,一旦将您的控件作为子控件添加到页面或页面上的另一个控件,您的控件将赶上"当前页面事件.

And that's it! Once you compile the DLL and include it in another project, you can instantiate instances of your user controls using the "new" operator, like any other class. As always, your control will be "caught up" to the current page event once added as a child control to the page or another control on the page.

这篇关于将 web 应用程序项目 ascx 编译成 dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将 web 应用程序项目 ascx 编译成 dll

基础教程推荐