ASP.Net中的Web Resource

來源:互聯網
上載者:User

標籤:style   blog   class   code   c   java   

http://support.microsoft.com/kb/910442,這是中文的,機器翻譯的,不太容易看懂,英文的是:http://support.microsoft.com/kb/910442/en-us。

記錄幾個要點:

  1. Web Resource是什嗎?解決什麼問題?Web Resource使用一種稱為WebResouce.axd的處理常式(Hanlder),該處理常式用來從Assembly中檢索抽取(retrieve)靜態資源檔案並返回給瀏覽器。處理常式WebResource.axd的類型是AssemblyResourceLoader。
  2. Web Resource是怎樣解決這些問題的(Web Resource是如何工作的How Web Resource Work)?也就是Web Resource是怎樣根據請求從Assembly中檢索抽取靜態檔案並返回給用戶端的(分解開來,這裡有兩個個問題(1)針對WebResouce.axd的請求時如何產生的;(2)處理常式WebResource.axd處理該請求的具體過程是怎樣的)?針對前面的問題(2)(問題(1)的答案見下面的第4點),回答如下:When a request comes in from the client for WebResource.axd, the handler looks for the Web Resource identifier in theQueryString method of the Request object(當一個針對WebResource.axd的請求從用戶端進來時,WebResource.axd處理常式從該請求的QueryString方法中尋找Web Resource identifier). Based on the value of the Web Resource identifier, the handler then tries to load the assembly that contains this resource(然後,WebResource.axd試圖載入包含Web Resource identifier所指資源的Assembly). If this operation is successful, the handler will then look for the assembly attribute and load the resource stream from the assembly. Finally, the handler will grab the data from the resource stream and send it to the client together with the content type that you specify in the assembly attribute。The URL for WebResource.axd looks like the following:
    WebResource.axd?d=SbXSD3uTnhYsK4gMD8fL84_mHPC5jJ7lfdnr1_WtsftZiUOZ6IXYG8QCXW86UizF0&t=632768953157700078
    The format of this URL is WebResource.axd?d=encrypted identifier&t=time stamp value. The "d" stands for the requested Web Resource. The "t" is the timestamp for the requested assembly, which can help in determining if there have been any changes to the resource.
  3. 如何將靜態資源檔案嵌入Assembly?
  4. 如何從Assembly中取出靜態資源檔案(即如何產生對處理常式WebResource.axd的請求)?For getting the Web Resource, I have used the GetWebResourceUrl method, which is a method of the ClientScriptManagerclass that is typically used for managing client-side scripts. This method returns a URL reference to the server-side resource that is embedded in an assembly. The GetWebResourceUrl method accepts the following two parameters:
    • Type: The type of the server-side resource
    • Resource Name: The name of the server-side resource
    To use this method, first you have to create an instance of the ClientScriptManager class and get the type of the class as shown below.When you have an instance of this class, you then have to call this method and pass the appropriate parameters as shown below, where I create a HyperLink button, and set the NavigateURL method to point to an embedded HTML resource.
     1 HyperLink hlHelpFile = new HyperLink(); 2 hlHelpFile.NavigateUrl = cs.GetWebResourceUrl(rsType, "SimpleControl.Help.htm"); 3 hlHelpFile.Attributes.Add("onmouseover", "ChangeImage(‘image1‘,‘Red‘)"); 4 hlHelpFile.Attributes.Add("onmouseout", "RollbackImage(‘image1‘,‘Green‘)"); 5              6 this.Controls.Add(hlHelpFile); 7  8 Image imgTest = new Image(); 9 imgTest.ImageUrl = cs.GetWebResourceUrl(rsType, "SimpleControl.smallFail.gif");10 imgTest.ID = "image1";11 hlHelpFile.Controls.Add(imgTest);


    回到上面1的問題,Web Resouce解決什麼問題?解決的是為用戶端返回資源檔的問題,所以如果你知道了資源檔的位置(也就是說如果你請求的資源檔不再Assembly中),完全沒有必要使用Web Resource。

     

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.