使用ASP.NET 2.0提供的WebResource管理资源
2009-06-23 14:24
169 查看
使用ASP.NET 2.0提供的WebResource管理资源
ASP.NET(1.0/1.1)给我们提供了一个开发WebControl的编程模型,于是我们摆脱了asp里面的include模式的复用方式。不过1.0/1.1提供的Web控件开发模型对于处理没有image、css等外部资源的组件还算比较得心应手,script虽然很多时候也是外部资源,但在开发控件的时候我们习惯把script使用Page.Register...Script()来嵌入模块,因为紧凑的东西更便于我们复用,用一个dll就可以解决问题又何必要节外生枝呢。
ASP.NET 2.0提供的Web Resources管理模型,很好的解决了image、css、script等外部资源的管理问题。现在只需要在solution explorer把资源文件的build action属性设为Embedded Resource。然后在assemblyinfo.cs里添加一句:
[assembly: WebResource("WebCtrl.cutecat.jpg", "image/jpg")]
我们可以看msdn里有WebResource的参数说明:第一个是资源的名字,第二个是资源的mime-type名。
其实这个语句放任何cs文件里,保证放在最高级namespace外就行。
然后在程序中调用如下:
m_Image.ImageUrl = this.Page.GetWebResourceUrl(typeof(WebCustom), "WebCtrl.cutecat.jpg");
GetWebResourceUrl的第一个参数是用户定义的类型(这个是用来确定assembly用的),第二个参数是资源名。
上面的语句返回给browser的代码是:
<img src="WebResource.axd?a=pWebCtrl&r=WebCtrl.cutecat.jpg&t=632390947985312500" style="border-width:0px;" />
其中的src就是GetWebesourceUrl执行后返回的,它有3个参数(这里的&被解析成了&,不过IIS也认的),第一个参数a是就是通过typeof(WebCustom)来确定的assembly的名字,第二个参数r很明显就是资源的名字了,第三个参数t是一个a所指的assembly的timestamp。这个t是为了让资源的引用能享用browser缓存的优化,因为IE对相同的url有自己的cache机制。又因为这个r同时又是用户assembly文件的timestamp,如果用户更新了代码,重新编译后t也会变化,这样也就保证了browser能获得最新的资源更新。如果我们能确定嵌入资源是确实不用再更新的,我们可以在typeof()里写一个bcl里的类型,比如typeof(string),那么他将只在freamwork升级后才会变动这个t。
当然这个WebResource.axd是不存在的,它只是IIS中的一个ISAPI影射。
使用示例代码如下:
![](Images/OutliningIndicators/ContractedBlock.gif)
![](Images/OutliningIndicators/ExpandedBlockStart.gif)
WebResource Demo#region WebResource Demo
![](Images/OutliningIndicators/InBlock.gif)
![](Images/OutliningIndicators/InBlock.gif)
using System;
![](Images/OutliningIndicators/InBlock.gif)
using System.Collections.Generic;
![](Images/OutliningIndicators/InBlock.gif)
using System.ComponentModel;
![](Images/OutliningIndicators/InBlock.gif)
using System.Text;
![](Images/OutliningIndicators/InBlock.gif)
using System.Web.UI;
![](Images/OutliningIndicators/InBlock.gif)
using System.Web.UI.WebControls;
![](Images/OutliningIndicators/InBlock.gif)
![](Images/OutliningIndicators/InBlock.gif)
[assembly: WebResource("WebCtrl.cutecat.jpg", "image/jpg")]
![](Images/OutliningIndicators/InBlock.gif)
![](Images/OutliningIndicators/InBlock.gif)
namespace WebCtrl
![](Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](Images/OutliningIndicators/InBlock.gif)
[DefaultProperty("Text")]
![](Images/OutliningIndicators/InBlock.gif)
[ToolboxData("<{0}:WebCustom runat=server></{0}:WebCustom>")]
![](Images/OutliningIndicators/InBlock.gif)
public class WebCustom : WebControl
![](Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](Images/OutliningIndicators/InBlock.gif)
private string text;
![](Images/OutliningIndicators/InBlock.gif)
private Image m_Image;
![](Images/OutliningIndicators/InBlock.gif)
![](Images/OutliningIndicators/InBlock.gif)
[Bindable(true)]
![](Images/OutliningIndicators/InBlock.gif)
[Category("Appearance")]
![](Images/OutliningIndicators/InBlock.gif)
[DefaultValue("")]
![](Images/OutliningIndicators/InBlock.gif)
public string Text
![](Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](Images/OutliningIndicators/ContractedSubBlock.gif)
get { return text; }
![](Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](Images/OutliningIndicators/ContractedSubBlock.gif)
set { text = value; }
![](Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](Images/OutliningIndicators/InBlock.gif)
![](Images/OutliningIndicators/InBlock.gif)
protected override void CreateChildControls()
![](Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](Images/OutliningIndicators/InBlock.gif)
m_Image = new Image();
![](Images/OutliningIndicators/InBlock.gif)
this.Controls.Add(m_Image);
![](Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](Images/OutliningIndicators/InBlock.gif)
![](Images/OutliningIndicators/InBlock.gif)
protected override void Render(HtmlTextWriter output)
![](Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](Images/OutliningIndicators/InBlock.gif)
m_Image.ImageUrl = this.Page.GetWebResourceUrl(typeof(WebCustom), "WebCtrl.cutecat.jpg");
![](Images/OutliningIndicators/InBlock.gif)
this.RenderChildren(output);
![](Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](Images/OutliningIndicators/ExpandedBlockEnd.gif)
#endregion
本文来自CSDN博客,转载请标明出处:/article/4588961.html
ASP.NET(1.0/1.1)给我们提供了一个开发WebControl的编程模型,于是我们摆脱了asp里面的include模式的复用方式。不过1.0/1.1提供的Web控件开发模型对于处理没有image、css等外部资源的组件还算比较得心应手,script虽然很多时候也是外部资源,但在开发控件的时候我们习惯把script使用Page.Register...Script()来嵌入模块,因为紧凑的东西更便于我们复用,用一个dll就可以解决问题又何必要节外生枝呢。
ASP.NET 2.0提供的Web Resources管理模型,很好的解决了image、css、script等外部资源的管理问题。现在只需要在solution explorer把资源文件的build action属性设为Embedded Resource。然后在assemblyinfo.cs里添加一句:
[assembly: WebResource("WebCtrl.cutecat.jpg", "image/jpg")]
我们可以看msdn里有WebResource的参数说明:第一个是资源的名字,第二个是资源的mime-type名。
其实这个语句放任何cs文件里,保证放在最高级namespace外就行。
然后在程序中调用如下:
m_Image.ImageUrl = this.Page.GetWebResourceUrl(typeof(WebCustom), "WebCtrl.cutecat.jpg");
GetWebResourceUrl的第一个参数是用户定义的类型(这个是用来确定assembly用的),第二个参数是资源名。
上面的语句返回给browser的代码是:
<img src="WebResource.axd?a=pWebCtrl&r=WebCtrl.cutecat.jpg&t=632390947985312500" style="border-width:0px;" />
其中的src就是GetWebesourceUrl执行后返回的,它有3个参数(这里的&被解析成了&,不过IIS也认的),第一个参数a是就是通过typeof(WebCustom)来确定的assembly的名字,第二个参数r很明显就是资源的名字了,第三个参数t是一个a所指的assembly的timestamp。这个t是为了让资源的引用能享用browser缓存的优化,因为IE对相同的url有自己的cache机制。又因为这个r同时又是用户assembly文件的timestamp,如果用户更新了代码,重新编译后t也会变化,这样也就保证了browser能获得最新的资源更新。如果我们能确定嵌入资源是确实不用再更新的,我们可以在typeof()里写一个bcl里的类型,比如typeof(string),那么他将只在freamwork升级后才会变动这个t。
当然这个WebResource.axd是不存在的,它只是IIS中的一个ISAPI影射。
使用示例代码如下:
![](Images/OutliningIndicators/ContractedBlock.gif)
![](Images/OutliningIndicators/ExpandedBlockStart.gif)
WebResource Demo#region WebResource Demo
![](Images/OutliningIndicators/InBlock.gif)
![](Images/OutliningIndicators/InBlock.gif)
using System;
![](Images/OutliningIndicators/InBlock.gif)
using System.Collections.Generic;
![](Images/OutliningIndicators/InBlock.gif)
using System.ComponentModel;
![](Images/OutliningIndicators/InBlock.gif)
using System.Text;
![](Images/OutliningIndicators/InBlock.gif)
using System.Web.UI;
![](Images/OutliningIndicators/InBlock.gif)
using System.Web.UI.WebControls;
![](Images/OutliningIndicators/InBlock.gif)
![](Images/OutliningIndicators/InBlock.gif)
[assembly: WebResource("WebCtrl.cutecat.jpg", "image/jpg")]
![](Images/OutliningIndicators/InBlock.gif)
![](Images/OutliningIndicators/InBlock.gif)
namespace WebCtrl
![](Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](Images/OutliningIndicators/InBlock.gif)
[DefaultProperty("Text")]
![](Images/OutliningIndicators/InBlock.gif)
[ToolboxData("<{0}:WebCustom runat=server></{0}:WebCustom>")]
![](Images/OutliningIndicators/InBlock.gif)
public class WebCustom : WebControl
![](Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](Images/OutliningIndicators/InBlock.gif)
private string text;
![](Images/OutliningIndicators/InBlock.gif)
private Image m_Image;
![](Images/OutliningIndicators/InBlock.gif)
![](Images/OutliningIndicators/InBlock.gif)
[Bindable(true)]
![](Images/OutliningIndicators/InBlock.gif)
[Category("Appearance")]
![](Images/OutliningIndicators/InBlock.gif)
[DefaultValue("")]
![](Images/OutliningIndicators/InBlock.gif)
public string Text
![](Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](Images/OutliningIndicators/ContractedSubBlock.gif)
get { return text; }
![](Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](Images/OutliningIndicators/ContractedSubBlock.gif)
set { text = value; }
![](Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](Images/OutliningIndicators/InBlock.gif)
![](Images/OutliningIndicators/InBlock.gif)
protected override void CreateChildControls()
![](Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](Images/OutliningIndicators/InBlock.gif)
m_Image = new Image();
![](Images/OutliningIndicators/InBlock.gif)
this.Controls.Add(m_Image);
![](Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](Images/OutliningIndicators/InBlock.gif)
![](Images/OutliningIndicators/InBlock.gif)
protected override void Render(HtmlTextWriter output)
![](Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](Images/OutliningIndicators/InBlock.gif)
m_Image.ImageUrl = this.Page.GetWebResourceUrl(typeof(WebCustom), "WebCtrl.cutecat.jpg");
![](Images/OutliningIndicators/InBlock.gif)
this.RenderChildren(output);
![](Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](Images/OutliningIndicators/ExpandedBlockEnd.gif)
#endregion
本文来自CSDN博客,转载请标明出处:/article/4588961.html
相关文章推荐
- 使用ASP.NET 2.0提供的WebResource管理资源
- 使用ASP.NET 2.0提供的WebResource管理资源
- 使用ASP.NET 2.0提供的WebResource管理资源-z
- 使用ASP.NET 2.0提供的WebResource管理资源
- 使用ASP.NET 2.0提供的WebResource管理资源
- 使用ASP.NET 2.0提供的WebResource管理资源
- 使用ASP.NET 2.0提供的WebResource管理资源
- 使用 ASP.NET 2.0提供的WebResource管理资源
- 最新版FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用简解(提供博客园本地下载)
- 使用ASP.NET 2.0提供的WebResource管理内嵌资源(c#)
- 使用ASP.NET 2.0提供的WebResource管理内嵌资源(c#)
- 使用ASP.NET 2.0提供的WebResource管理内嵌资源(c#)
- 使用ASP.NET 2.0提供的WebResource管理嵌入资源
- 使用ASP.NET 2.0提供的WebResource管理内嵌资源(c#)
- 最新版FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用简解(提供博客园本地下载)
- 最新版FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用简解(提供博客园本地下载)
- 最新版FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用简解(提供博客园本地下载)
- 使用ASP.NET 2.0提供的WebResource管理内嵌资源(c#)
- 最新版FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用简解(提供博客园本地下载)
- 最新版FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用简解(提供博客园本地下载)