如何使用ASP.NET2.0的“嵌入的资源”(转)

简介: 这里我们要介绍的内容将让这些资源变得更加简洁。 通常我们在ASP.NET2.0中使用嵌入的资源的时候只需完成以下几步: 1.添加资源文件,如: 2.将资源文件的编译方式变为“嵌入的资源”,如: 3.

这里我们要介绍的内容将让这些资源变得更加简洁。

通常我们在ASP.NET2.0中使用嵌入的资源的时候只需完成以下几步:

1.添加资源文件,如:

resource

2.将资源文件的编译方式变为“嵌入的资源”,如:

embededResource

3.添加Assembly信息(AssemblyInfo.CS或者在具体类的namespace之上),如:

[assembly: System.Web.UI.WebResource("IntelligenceTextBox.JScript.IntelligenceTextBox.js", "application/x-javascript")]
[assembly: System.Web.UI.WebResource("IntelligenceTextBox.CSS.IntelligenceTextBoxStylesheet.css", "text/css")]

4.将资源文件注册到页面文件中(在protected override void OnPreRender(System.EventArgs e)里),如:

Page.ClientScript.RegisterClientScriptResource(this.GetType(), "IntelligenceTextBox.JScript.IntelligenceTextBox.js");

完成这一步后的脚本文件就会在PreRender的时候输出到前台Html中。如:

<script src="/WebResource.axd?d=XBIPl09lmgYKinSg7vem6zAjPh9zda0B5YvbMz9cdk-Dtoq3pnz_VUoa1-xOFpiq0&amp;t=633419848307656250" type="text/javascript"></script>
这样就可以在页面文件中引用相关的资源文件中的内容了。
但是我们注意到RegisterClientScriptResource在生成的时候都会当作application/x-javascript来输出,因此最终都只能得到type="text/javascript",而这样的设置则不符合其他类型资源的输入规则。为此我构建了下面这个类,仅完成了MetaType类型为text/css的资源的输出,但它很容易扩充成支持各种格式的类型,而扩充需要您做的事很少。引入资源的方式却十分简单。
思路:
添加资源到页面,无非就是做到如上所示的1到4点,而唯一要解决的就是不同的metaType所特定的格式不同,如:
CSS:
<link href="{0}" rel="stylesheet" type="text/css"/>
JavaScript:
<script src="{0}" type="text/javascript"></script>
而我们的期待的表现形式则形如上文第4点所示的方式,另外需要解决的一个问题是一个页面多个资源不用重复注册的问题。
是否添加重复资源的问题应该留给用户自行解决,因此通过提供IsResourceRegistered方法给用户进行自行判断。
[下面的代码需要经过改造后才能通过.NET2.0编译器的编译,否则默认使用.NET3.0以上编译器可以编译,请见谅!]
调用代码(示例):
        protected override void OnPreRender(System.EventArgs e)
        {
            base.OnPreRender(e);
            if (Page != null)
            {
                ClientScriptHelper cs = new ClientScriptHelper(this.Page);

                string cssName = "IntelligenceTextBox.CSS.IntelligenceTextBoxStylesheet.css";
                if (!cs.IsResourceRegistered(cssName))
                    cs.RegisterResource<IntelligenceTextBox>(this, ClientScriptHelper.MetaType.TextCSS, cssName);
                //Page.ClientScript.RegisterClientScriptResource(this.GetType(), "IntelligenceTextBox.JScript.IntelligenceTextBox.js");
                string jsName = "IntelligenceTextBox.JScript.IntelligenceTextBox.js";
                if (!cs.IsResourceRegistered(jsName))
                cs.RegisterResource<IntelligenceTextBox>(this, 
                    ClientScriptHelper.MetaType.TextJavaScript,
                    jsName);
            }
        }
源代码:
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace IntelligenceTextBox
{
    internal class ClientScriptHelper
    {
        private IDictionary resourceActuals;

        /// <summary>
        /// Get the parent container (page) reference.
        /// </summary>
        /// <param name="container"></param>
        public ClientScriptHelper(Page container)
        {
            resourceActuals = container.Items;
        }

        /// <summary>
        /// Reigister the resource to the page.
        /// </summary>
        /// <typeparam name="T">The type of resource.</typeparam>
        /// <param name="sender">The resource.</param>
        /// <param name="metaType">The metaType of the resource.</param>
        /// <param name="resourceName">The name of the resource.</param>
        public void RegisterResource<T>(T sender, MetaType metaType, string resourceName)
            where T : Control
        {
            ResourceInfo resourceInfo = ResourceHtmlTemplateFactory(metaType, sender.Page);
            if (sender != null)
            {
                Literal resourceLiteral = new Literal();
                resourceLiteral.Text = string.Format(resourceInfo.HtmlTemplate,
                    sender.Page.ClientScript.GetWebResourceUrl(typeof(T), resourceName)
                );

                resourceInfo.Where.Controls.Add(resourceLiteral);

                if (!resourceActuals.Contains(resourceName))
                    resourceActuals.Add(resourceName, resourceLiteral);
            }
        }

        /// <summary>
        /// Make sure is the resource has been registered in the current Page.
        /// </summary>
        /// <param name="resourceName">The name of the resource.</param>
        /// <returns></returns>
        public bool IsResourceRegistered(string resourceName)
        {
            return resourceActuals.Contains(resourceName);
        }

        /// <summary>
        /// The factory to create the right ResourceInfo for render.
        /// </summary>
        /// <param name="metaType">MetaType of the resource.</param>
        /// <param name="container">The page will contain the resource</param>
        /// <returns></returns>
        private static ResourceInfo ResourceHtmlTemplateFactory(MetaType metaType, Page container)
        {
            ResourceInfo resource = new ResourceInfo()
            {
                HtmlTemplate = string.Empty,
                Where = container.Header
            };

            if (metaType == MetaType.TextCSS)
            {
                resource.HtmlTemplate = "\n<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\"/>";
                resource.Where = container.Header;
            }
            else if (metaType == MetaType.TextJavaScript)
            {
                resource.HtmlTemplate = "\n<script src=\"{0}\" type=\"text/javascript\"></script>";
                resource.Where = container.Header;
            }

            //Todo: Add the other metatype ResourceInfo instance.
            
            return resource;
        }

        /// <summary>
        /// The infomation of resource depend on the metatype. 
        /// </summary>
        internal class ResourceInfo
        {
            /// <summary>
            /// The html syntax of the resource.
            /// e.g.
            /// text/css: \n<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\"/>
            /// </summary>
            public string HtmlTemplate { get; set; }

            /// <summary>
            /// The place to render the html syntax.
            /// </summary>
            public System.Web.UI.HtmlControls.HtmlControl Where { get; set; }
        }

        /// <summary>
        /// MetaType
        /// </summary>
        public enum MetaType
        { 
            TextCSS,
            TextJavaScript
        }
    }
}
本文转自:http://www.cnblogs.com/volnet/archive/2008/03/24/1120314.html(感谢作者)

博客园大道至简

http://www.cnblogs.com/jams742003/

转载请注明:博客园

目录
相关文章
|
开发框架 前端开发 JavaScript
盘点72个ASP.NET Core源码Net爱好者不容错过
盘点72个ASP.NET Core源码Net爱好者不容错过
369 0
|
开发框架 .NET
ASP.NET Core NET7 增加session的方法
ASP.NET Core NET7 增加session的方法
152 0
|
存储 开发框架 前端开发
asp.net与asp.net优缺点及示例
asp.net与asp.net优缺点及示例
245 0
|
11月前
|
传感器 人工智能 供应链
.NET开发技术在数字化时代的创新作用,从高效的开发环境、强大的性能表现、丰富的库和框架资源等方面揭示了其关键优势。
本文深入探讨了.NET开发技术在数字化时代的创新作用,从高效的开发环境、强大的性能表现、丰富的库和框架资源等方面揭示了其关键优势。通过企业级应用、Web应用及移动应用的创新案例,展示了.NET在各领域的广泛应用和巨大潜力。展望未来,.NET将与新兴技术深度融合,拓展跨平台开发,推动云原生应用发展,持续创新。
132 4
|
12月前
|
数据库连接 开发者
.NET 内存管理两种有效的资源释放方式
【10月更文挑战第15天】在.NET中,有两种有效的资源释放方式:一是使用`using`语句,适用于实现`IDisposable`接口的对象,如文件流、数据库连接等,能确保资源及时释放,避免泄漏;二是手动调用`Dispose`方法并处理异常,提供更灵活的资源管理方式,适用于复杂场景。这两种方式都能有效管理资源,提高应用性能和稳定性。
280 2
|
12月前
|
算法 Java 数据库连接
.NET 内存管理两种有效的资源释放方式
【10月更文挑战第14天】在 .NET 中,`IDisposable` 接口提供了一种标准机制来释放非托管资源,如文件句柄、数据库连接等。此类资源需手动释放以避免泄漏。实现 `IDisposable` 的类可通过 `Dispose` 方法释放资源。使用 `using` 语句可确保资源自动释放。此外,.NET 的垃圾回收器会自动回收托管对象所占内存,提高程序效率。示例代码展示了如何使用 `MyFileHandler` 类处理文件操作并释放 `FileStream` 资源。
201 2
|
开发框架 前端开发 .NET
究竟是什么让.NET 开发者社区拥有如此强大的力量?资源、分享还是成长的秘密?
【8月更文挑战第28天】.NET开发者社区为成员提供了丰富的资源、积极的分享氛围和广阔的成长空间,是一个充满活力的知识宝库。在这里,从前沿的开源项目到深入的技术解析应有尽有,无论你是新手还是专家,都能找到适合自己的学习与交流机会,共同推动.NET技术的发展。
90 5
|
开发框架 JSON .NET
ASP.NET Core 标识(Identity)框架系列(三):在 ASP.NET Core Web API 项目中使用标识(Identity)框架进行身份验证
ASP.NET Core 标识(Identity)框架系列(三):在 ASP.NET Core Web API 项目中使用标识(Identity)框架进行身份验证
186 1
|
开发框架 .NET 数据库连接
ASP.NET Core 标识(Identity)框架系列(一):如何使用 ASP.NET Core 标识(Identity)框架创建用户和角色?
ASP.NET Core 标识(Identity)框架系列(一):如何使用 ASP.NET Core 标识(Identity)框架创建用户和角色?
234 0
|
存储 分布式计算 大数据
MaxCompute操作报错合集之自定义udf的函数,引用了import net.sourceforge.pinyin4j.PinyinHelper;但是上传资源后,出现报错,是什么原因
MaxCompute是阿里云提供的大规模离线数据处理服务,用于大数据分析、挖掘和报表生成等场景。在使用MaxCompute进行数据处理时,可能会遇到各种操作报错。以下是一些常见的MaxCompute操作报错及其可能的原因与解决措施的合集。
288 0