我正在尝试通过GetWebResourceUrl获取嵌入式图像资源网址。
情况:
编写Visual Studio包(扩展名)。单击我的菜单按钮,我想复制位于AssemblyName1
的所有嵌入资源。在资源类型text/css
我有这样的网址:
background-image: url('<%=WebResource("PSK.Web.UI.Controls.PSKSoftApp.Css.AppHeader.sprite.png")%>');
复制此类资源时,我希望通过WebResource对其进行预编译,将其转换为:
background-image: url('/WebResource.axd?d=PhPk80h_UWEcbheb-NHNP5WshV_47UOpWqAOl1_li
UFfN4cNofL74cFlQ1fvpFSf0&t=632573240669964903')
我将程序集(AssemblyName1
)连接到我的VSPackage
程序集。
在AssemblyName1
:
[assembly: WebResource("PSK.Web.UI.Controls.PSKSoftApp.Css.AppHeader.sprite.png", "image/png")]
PSK.Web.UI.Controls.PSKSoftApp.Css.AppHeader.sprite.png
添加到嵌入资源。
在VSPackage程序集中,我写道:
System.Reflection.MemberInfo[] methodCandidates = typeof(System.Web.Handlers.AssemblyResourceLoader).GetMember("GetWebResourceUrlInternal",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
foreach (var methodCandidate in methodCandidates)
{
var method = methodCandidate as System.Reflection.MethodInfo;
if (method == null || method.GetParameters().Length != 5) continue;
object result = method.Invoke(null,
new object[]
{
System.Reflection.Assembly.GetAssembly(typeof(PSK.Web.UI.Controls.PSKSoftApp.PSKSoftApp)),
"PSK.Web.UI.Controls.PSKSoftApp.Css.AppHeader.sprite.png",
false,
false,
null
});
}
致电
method.Invoke(null, new object[] {System.Reflection.Assembly.GetAssembly(typeof(PSK.Web.UI.Controls.PSKSoftApp.PSKSoftApp)),
"PSK.Web.UI.Controls.PSKSoftApp.Css.AppHeader.sprite.png", false, false, null});
返回异常:{"Value cannot be null.\r\nParameter name: basepath"}
这是什么 BasePath ,我应该在哪里设置它?
答案 0 :(得分:0)
由于这是谷歌的最高评论,我找不到相关的答案,这里是我发现的原因和解决方案(对我而言):
情况:尝试在我构建的服务器控件上对一些公共方法进行单元测试,其中包括对WebResource的请求。
错误:“basepath”为空
原因:控件向页面请求WebResource。页面要求ScriptManager请求所述资源。 ScriptManager执行一些内部操作并尝试请求WebResource但遇到“basepath”问题。
由于这是一个单元测试,因此没有Web服务器。没有Web服务器意味着脚本管理器不知道要为其发出请求的“基本路径”。没有“basepath”意味着它无法构造所述资源的HTTP请求。
解决方案:两个选项:
A)在单元测试中模拟/注入Web服务器,以允许Page / ScriptManager生成BasePath并发出适当的请求。这有在线资源,但我找到的最好的(但没有使用)引用了[AspNetDevelopmentServer]属性的使用。我没有使用这个解决方案,因为我们有一个持续集成构建服务器,我不想让测试在所有开发人员机器和构建服务器上运行。这可能很容易,但我没有尝试过。
B)重新设计控件,以便在服务器控件的不可测试(不费力)部分中发出webresource请求。我正在使用CreateChildControls(自动调用)并将webresource请求移动到OnInit。可能不是最佳实践,但它对我有用。