Use webresource provided by ASP. NET 2.0 to manage embedded Resources

Source: Internet
Author: User
ASP. NET (1.0/1.1) provides us with a webcontrol programming model, so we get rid of the reuse of the include Mode in ASP. However, the web control development model provided by 1.0/1.1 is quite handy for components that do not have external resources such as image and CSS. Although scripts are often external resources, however, when developing controls, we are used to using pages for scripts. register... script () is used to embed modules. Because compact things are easier for us to reuse, a DLL can solve the problem.

The web resources management model provided by ASP. NET 2.0 can effectively manage external resources such as image, CSS, and script. Now you only need to set the build action attribute of the resource file to embedded resource in Solution Explorer. Then add the following sentence to assemblyinfo. CS:[Assembly: webresource ("Webctrl.cutecat.jpg","Image/jpg")]

We can see that there is a webresource parameter description in msdn: the first is the resource name, and the second is the resource mime-type name.
In fact, this statement is stored in any CS file to ensure that it is placed outside the highest level of namespace.

ThenProgramIs called as follows:M_image.imageurl= This. Page. getwebresourceurl (Typeof(Webcustom ),"Webctrl.cutecat.jpg");

The first parameter of getwebresourceurl is the user-defined type (this is used to determine the Assembly), and the second parameter is the resource name.

The preceding statement is returned to the browserCodeYes:<IMGSRC= "Webresource. axd? A?pwebctrl&amp;r=webctrl.cutecat.jpg & amp; T = 632390947985312500"Style= "Border-width: 0px ;" />

The SRC is returned after the getwebesourceurl is executed. It has three parameters (here & is parsed as & amp;, but it is also recognized by IIS ), the first parameter A is the name of the Assembly determined through typeof (webcustom). The second parameter R is obviously the name of the resource, the third parameter T is the timestamp of the Assembly referred to by. This T is used to optimize browser cache for resource reference, because IE has its own cache mechanism for the same URL. This R is also the timestamp of the user Assembly file. If the user updates the code, t will change after re-compilation, which ensures that the browser can obtain the latest resource update. If we can determine that the embedded resources do not need to be updated, we can write a type in BCl in typeof (), such as typeof (string ), then, the T will be changed only after the freamwork upgrade.

Of course, this webresource. axd does not exist. It is only an ISAPI in IIS.

The sample code is as follows: Webresource demo # Region Webresource demo

Using System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. text;
Using System. Web. UI;
Using System. Web. UI. webcontrols;

[Assembly: webresource ( " Webctrl.cutecat.jpg " , " Image/jpg " )]

Namespace Webctrl
{
[Defaultproperty ( " Text " )]
[Toolboxdata ( " <{0}: webcustom runat = Server> </{0}: webcustom> " )]
Public   Class Webcustom: webcontrol
{
Private   String Text;
Private Image m_image;

[Bindable ( True )]
[Category ( " Appearance " )]
[Defaultvalue ( "" )]
Public   String Text
{
Get   {ReturnText ;}
Set   {Text=Value ;}
}

Protected   Override   Void Createchildcontrols ()
{
M_image= NewImage ();
This. Controls. Add (m_image );
}

Protected   Override   Void Render (htmltextwriter output)
{
M_image.imageurl= This. Page. getwebresourceurl (Typeof(Webcustom ),"Webctrl.cutecat.jpg");
This. Renderchildren (output );
}
}
}
# Endregion

Related Article

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.