繁体   English   中英

无法访问asp.net中的嵌入式资源

[英]Cannot access embedded resource in asp.net

我正在尝试将图像和样式表从用户控件移动到程序集中的嵌入资源。 我使用Reflector看到图像和.css文件嵌入在程序集中,但是当我尝试使用ClientScript.GetWebResourceUrl()创建的URL访问它们时,找不到该资源。 我很难过。

程序集默认命名空

TestWebApp

文件的路径(标记为BuildAction:Embedded Resource)是

TestWebApp/Resources/CSS/PaymentHistory.css
TestWebApp/Resources/Images/loading.gif

所以我的资源注册为:

[assembly: WebResource("TestWebApp.Resources.CSS.PaymentHistory.css", "text/css", PerformSubstitution = true)]
[assembly: WebResource("TestWebApp.Resources.Images.loading.gif", "image/gif")]

访问资源的用户控件(在同一程序集中):

TestWebApp.UserControls.PaymentHistory

为了简化,我目前只是尝试引用图像而不是样式表。 在我的用户控件的Page_Load中,我将Image控件的ImageUrl设置为资源URL:

image1.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "TestWebApp.Resources.Images.loading.gif");

在运行时,一切看起来都没有错误,但它会渲染出损坏的图像。 这是渲染的图像源:

<img style="border-width:0px;" src="/WebResource.axd?d=8fC_1tLPjrUCxmFc_Q2MKY0-pHAak-sTWkpLLV3D56H_c08LujXC63ia2PNICE65_i-Q4JqprAigLpbrXG-rIAr6ePO4HHcdQKgdd3szlThv2gizxOJLJsPRNe-b_M6ApTwPsH_5oZAuONTN0cumOTRr1nA1&amp;t=635133745137507721" id="ph1_image1">

如果我在浏览器中导航到该URL,则会收到404,无法找到该资源。 我究竟做错了什么?

编辑:必须有一些基本的我不理解和/或我正在做一些非常愚蠢的事情。 这是一个简单的VS 2010示例 我已经遵循了所有必要的步骤,我知道嵌入JScript1.js并通过WebResource.axd访问它,但它得到了错误。

在示例项目的Default.aspx.cs文件中,将this.GetType()更改为typeof(_Default)

Page.ClientScript.RegisterClientScriptInclude("JScript1",
    Page.ClientScript.GetWebResourceUrl(typeof(_Default), "EmbeddedResources.JScript1.js"));

同样,在PaymentHistory.ascx.cs文件中,将this.GetType()更改为typeof(PaymentHistory)

image1.ImageUrl = Page.ClientScript.GetWebResourceUrl(
    typeof(PaymentHistory), "TestWebApp.Resources.Images.loading.gif");

Explanation: GetWebResourceUrl查看type参数以确定哪个程序集包含嵌入的资源。 指定this.GetType()作为type是不正确的,因为在.aspx或.ascx代码隐藏类中, this.GetType() 不是指该类, this.GetType()指从.aspx动态生成的派生类或.ascx标记。 此派生类驻留在单独的程序GetWebResourceUrl ,因此GetWebResourceUrl无法找到嵌入的资源。

资源是否与用户控件位于单独的项目或同一项目中? 如果单独,你必须用一个对象的GetType()函数替换this.GetType(),该函数驻留在单独的项目中。

如果在同一个项目中,只需执行Page.GetType(),因为您需要引用页面而不是用户控件

首先,您传递给GetWebResourceUrl调用的类型(或下面显示的RegisterClientScriptResource)实际上指向哪个程序集包含您的资源。 问题是“this.GetType()”返回的类型不在当前正在执行的程序集中(尽管可能很奇怪)。

以下两行证明了这个问题。

        Response.Write(this.GetType().Assembly.FullName + "<br>");
        Response.Write(Assembly.GetExecutingAssembly().FullName + "<br>");

第一行返回“App_Web _ ??????”的名称 部件。 第二个返回预期的“EmbeddedResources”程序集。

在下面的调用中,我只传入我从执行程序集中返回的第一个类型,并且调用正常。 Page.ClientScript.RegisterClientScriptResource(Assembly.GetExecutingAssembly()。GetTypes()[0],name [0]);

this.GetType()实际上返回Web服务器创建的类型继承的类型。 这就是为什么typeof(_Default)也可以用来指定正确的程序集。

在这里,您可以看到Vb.NET中的代码,它使用Reflection库来检测当前的Assembly Name和当前的NameSpace。

如果将命名空间与嵌入的图像名称连接,则可以使用命令Page.clientScript.GetWebResourceURL生成图像链接,如第一个函数中所示

在第二个函数中,您将看到所有资源名称中的循环,直到找到嵌入资源的完整名称。

Friend Class ReadResources

    ' Get our assembly.
    Private Shared executingAssembly As System.Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly()

    ' Get our namespace.
    Private Shared myNamespace As String = executingAssembly.GetName().Name.ToString()

    ''' <summary>
    ''' Generate resource link
    ''' </summary>
    Friend Shared Function GetResourceLink(ByVal ref As String,
                                           ByVal obj As Object,
                                           ByVal page As Web.UI.Page) As String
        Dim out As String = Nothing
        out = Page.ClientScript.GetWebResourceUrl(obj.GetType, myNamespace & "." & ref)

        If out Is Nothing OrElse out.Length <= 0 Then
            out = FindResource(ref, obj)
        End If

        Return out
    End Function

    Friend Shared Function FindResource(ByVal reference As String,
                                        ByVal obj As Object) As String
        Dim out As String = ""
        For Each embedded In obj.GetType().Assembly.GetManifestResourceNames()
            If embedded.Contains(reference) Then
                out = embedded
                Exit For
            End If
        Next
        Return out
    End Function


End Class

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM