Using webresource. axd for Embedded Resources

Source: Internet
Author: User

There is a great feature in ASP. net V2 that we can directly use embedded resources in compiled assemblies. for example, we have an image compiled in a assembly. without this feature, we might have to export the image to local file system, then can use it. but now, we can just simply invoke MethodPage. clientscript. getwebresourceurlTo obtain the URL to the embedded image. And all the other messes, such as performance, updating assemblies, Etc., can be ignored.

Using Web Resources

Because of its fascinating feature, web resources are welcome a lot. however, it is pretty much short of documentation as of now. it's my pleasure to introduce this great feature to everybody here given you're building a control and you want to embed an image in the compiled Assmbly. then you can follow the steps below:

  • Add the image to your project.
  • In the Solution Explorer, click the file, and in the "properties" window, change "build action" to "embedded resource ". do not use "add images to a resx file" approach here.
  • Add the following code to your assemblyinfo. CS file to mark it as Web-accessible: [Assembly: webresource ("mynamespace.imagename.gif", "image/GIF")]

    Important Note: The "mynamespace", indicating the embedded image's namespace, comes from the naming rule: the project's default namespace followed by the folder hierarchy.

  • UseGetwebresoureceurlMethod on page. clientscript to get a URL that can be rendered out to the client:

    String imageurl = page. clientscript. getwebresourceurl (typeof (mytype), "mynamespace.imagename.gif ");
  • Note that "mytype" and the embedded image "imagename.gif" shoshould be compiled in the same assembly.

    This results in a URL of the form:
    Webresource. axd? D = v7x1l5vnlo8zym8thy7drjadb8ahsegavvy601_ws4rleo9bl96l-_ n5gndtidvk0vylnj6jx8c3us0v3mzdww2 & t = 632590134445625000

    The "D" query string parameter stands for the embedded resource, and the "T" stands for the Assembly timestamp.

A cool tip

Given we have a Javascript file embedded in a assembly. the Javascript file will render a image for client users, and I 'd like to embed this image as a web resource as well. however, there is no way to get a static URL for the image.

ASP. NET V2 provides a "webresource" macro to resolve this problem.

  • In the assemblyinfo. CS, use

    [Assembly: webresource ("mynamespace.imagename.gif", "image/GIF", required msubstitution = true)]

    Instead:

    [Assembly: webresource ("mynamespace.imagename.gif", "image/GIF")]

    "Required msubstitution = true" means there are macros existed in the resource that need to be substituted in with real values.

  • In the resource file, use macro<% = Webresource ("mynamespace.imagename.gif") %>To replace the image's URL.

Now, you can find another webresource. axd URL, pointing to the image, in the Javascript file in your Web browers.

For now, "webresource ()" is the only macro you can place inside the "<% = %>" syntax within an embedded resource.

Cool huh?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.