天天看点

ASP.Net中的Web Resource

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

记录几个要点:

Web Resource是什么?解决什么问题?Web

Resource使用一种称为WebResouce.axd的处理程序(Hanlder),该处理程序用来从Assembly中检索抽取(retrieve)静态资源文件并返回给浏览器。处理程序WebResource.axd的类型是AssemblyResourceLoader。

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:

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.

如何将静态资源文件嵌入Assembly?

如何从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的问题,Web

Resouce解决什么问题?解决的是为客户端返回资源文件的问题,所以如果你知道了资源文件的位置(也就是说如果你请求的资源文件不再Assembly中),完全没有必要使用Web

Resource。