Unable to access embedded resource in asp.net

I am trying to move an image and a stylesheet from a user control into inline assembly resources. I used Reflector to see that the image and the .css file are embedded in the assembly, but when I try to access them using the URL generated by ClientScript.GetWebResourceUrl (), the resource was not found. I'm at a dead end.

The default namespace for the assembly is:

TestWebApp 

File Paths (marked as BuildAction: Embedded Resource)

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

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

A user control (in the same assembly) that accesses resources:

 TestWebApp.UserControls.PaymentHistory 

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

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

At runtime, everything seems to work without errors, but it creates a damaged image. Here is the rendering of the 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"> 

If I go to this URL in my browser, I get 404, the resource was not found. What am I doing wrong?

EDIT: There must be something fundamental, I don’t understand and / or do something really stupid. Here is a simple example of VS 2010 . I followed all the necessary steps that I know to implement JScript1.js and access it through WebResource.axd, but it gets an error.

+6
source share
4 answers

In the Default.aspx.cs file of your sample project, change this.GetType() to typeof(_Default) :

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

Similarly, in the file PaymentHistory.ascx.cs change this.GetType() to typeof(PaymentHistory) :

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

Explanation: GetWebResourceUrl considers the type argument to determine which assembly contains the embedded resource. Specifying this.GetType() as type is incorrect because in the .aspx or .ascx code this.GetType() code does not refer to this class, but rather to a derived class that is dynamically generated from .aspx or. ascx markup. This derived class is in a separate assembly, so GetWebResourceUrl cannot find the embedded resource.

+9
source

Is the resource in a separate project or in the same project as your User Control? Separately, you must substitute this.GetType () using the GetType () function of the object, which is in a separate project.

If in the same project just do Page.GetType (), because you need a link to the page, not to the user control

+1
source

First of all, the type that you pass to GetWebResourceUrl (or shown below in RegisterClientScriptResource below) actually indicates which assembly contains your resource. The problem is that "this.GetType ()" returns a type that is not in the current executing assembly (oddly enough).

The following two lines demonstrate this problem.

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

The first line returns the name "App_Web _ ??????" assembly. The second returns the expected assembly "EmbeddedResources".

In the call below, I just pass in the first type that I return from the executing assembly, and the call works. .ClientScript.RegisterClientScriptResource page (Assembly.GetExecutingAssembly (). GetTypes () [0], names [0]);

this.GetType () actually returns the type that the web server creates that inherits from your type. This is why typeof (_Default) will also work to indicate the correct assembly.

+1
source

Here you can see the code in Vb.NET that uses the Reflection library to detect the current assembly name and current NameSpace.

If you combine the namespace with the embedded image name, you can use the Page.clientScript.GetWebResourceURL command to create a link for the image, as you see in the first function

In the second function, you see a loop in all resource names until you find the full name of the built-in resouce.

 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 
0
source

Source: https://habr.com/ru/post/952760/


All Articles