How To Use An Embedded Resource From A Sub-Folder

If you’re creating a custom client-side control by deriving from System.Web.UI.IScriptControl, you may want to embed the .js files into your assembly instead of having to deploy them to every web site that uses those controls (this is especially valuable if you’re developing a control library).  All you need to do is to:

  1. Click on the .js file in the Solution Explore to select it, and then set Build Action (in the properties window) to Embedded Resource.

 

  1. Add the following attribute to your control code-behind file (e.g. MyControl.cs):

 

[assembly: System.Web.UI.WebResource("ControlLibrary.MyControl.js", "text/javascript")]

 

  1. In GetScriptReferences, instead of setting the Path property, use the parameterized constructor as follows:

 

public IEnumerable<ScriptReference> GetScriptReferences()
{
     return new ScriptReference[] { new ScriptReference("
ControlLibrary.MyControl.js", "ControlLibrary"
) };
}

 

However, if you place your JavaScript files in a subfolder (e.g. /ControlLibrary/Scripts/MyControl.js), then you will have to add the folder name to the dotted resource name above, e.g. ControlLibrary.Scripts.MyControl.js.

 

If you don’t add the folder name, you’ll probably see a run-time error message like this:

System.InvalidOperationException: Assembly ControlLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' contains a Web resource with name 'ControlLibrary.MyControl.js', but does not contain an embedded resource with name 'ControlLibrary.MyControl.js'.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值