无法访问在asp.net中嵌入的资源 [英] Cannot access embedded resource in asp.net

查看:177
本文介绍了无法访问在asp.net中嵌入的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的图像和样式表从用户控制在组装移动到嵌入的资源。我已经使用反射来看到图像和.css文件嵌入在组装,但是当我试图利用ClientScript.GetWebResourceUrl()创建的URL来访问它们,资源未找到。我难倒。

I'm trying to move an image and a style sheet from a user control to embedded resources in the assembly. I have used Reflector to see that the image and .css file are embedded in the assembly, but when I try to access them using the URL created by ClientScript.GetWebResourceUrl(), the resource is not found. I'm stumped.

大会的默认命名空间:

TestWebApp

该文件的路径(标记为BuildAction的:嵌入的资源)的

The paths to the files (marked as BuildAction: Embedded Resource) are

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

所以我的资源被注册为:

And so my resources are registered as:

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

这是访问资源的用户控件(在同一装配):

User Control (in the same assembly) that accesses the resources :

TestWebApp.UserControls.PaymentHistory

要简化,我目前只是想引用的图像,而不是样式表。在我的用户控件的Page_Load中,我Image控件的的ImageUrl设置为资源网址:

To simplify, I'm currently just trying to reference the image and not the stylesheet. In my user control's Page_Load, I set the ImageUrl of an Image control to the resource URL:

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

在运行时,一切似乎没有错误工作,但它呈现一个破碎的形象。下面是渲染图像源:

At runtime, everything appears to work without errors but it renders a broken image. Here is the rendered image source:

<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">

如果我浏览到我的浏览器,网址,我得到一个404,资源无法找到。我在做什么错了?

If I navigate to that URL in my browser, I get a 404, The resource cannot be found. What am I doing wrong?

编辑:
一定有什么根本的,我不理解和/或我在做一些非常愚蠢的。 下面是一个简单的VS 2010的例子 。我按照所有的必要的步骤,我所知道的嵌入JScript1.js并通过WebResource.axd的访问它,但它得到的错误。

There must be something fundamental I'm not understanding and/or I'm doing something really stupid. Here is a simple VS 2010 example. I have followed all of the required steps I'm aware of to embed JScript1.js and access it via WebResource.axd, but it gets the error.

推荐答案

在您的示例项目的Default.aspx.cs文件,更改 this.GetType() typeof运算(_Default)

In the Default.aspx.cs file of your example project, change this.GetType() to 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");

说明: <$c$c>GetWebResourceUrl着眼于键入参数来确定哪些程序集包含嵌入的资源。指定 this.GetType()键入不正确,因为,在.aspx或的.ascx code -behind类, this.GetType()指的的那个类,而是那个被动态地从在.aspx或产生的派生类。 ASCX标记。这个派生类驻留在一个单独的程序,所以 GetWebResourceUrl的找不到嵌入的资源。

Explanation: GetWebResourceUrl looks at the type argument to determine which assembly contains the embedded resource. Specifying this.GetType() as the type is incorrect because, in an .aspx or .ascx code-behind class, this.GetType() refers not to that class but rather to the derived class that gets dynamically generated from the .aspx or .ascx markup. This derived class resides in a separate assembly, so GetWebResourceUrl can't find the embedded resource.

这篇关于无法访问在asp.net中嵌入的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆