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

时间:2013-08-29 17:17:00

标签: c# asp.net asp.net-3.5 embedded-resource

我正在尝试将图像和样式表从用户控件移动到程序集中的嵌入资源。我使用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,无法找到该资源。我做错了什么?

修改 必须有一些基本的我不理解和/或我正在做一些非常愚蠢的事情。 Here is a simple VS 2010 example。我已经遵循了所有必要的步骤,我知道要嵌入JScript1.js并通过WebResource.axd访问它,但它会收到错误。

4 个答案:

答案 0 :(得分:11)

在示例项目的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");

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

首先,您传递给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)也可以用来指定正确的程序集。

答案 3 :(得分:0)

在这里,您可以看到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
相关问题