如何使用ASP.NET2.0的“內嵌資源”

來源:互聯網
上載者:User

關於內嵌資源在MSDN已經有一些基本的介紹:“內嵌資源”(ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/dv_csexpresscon/html/f42dff1c-6804-4fda-94e5-1e77460a9142.htm)

這裡我們要介紹的內容將讓這些資源變得更加簡潔。

通常我們在ASP.NET2.0中使用內嵌資源的時候只需完成以下幾步:

1.添加資源檔,如:

2.將資源檔的編譯方式變為“內嵌資源”,如:

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        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.