Introduction to the use and advantages of asp.net server-side command include

Source: Internet
Author: User

The server in asp.net contains a simple command <! -- # Include file | virtual = "filename"-> In this command, the glossary in msdn is:Insert the content of the specified file into the ASP. NET file, including the webpage (. aspx file), user control file (. ascx file), and Global. asax file.The basic function of inserting static files is not mentioned. inserting aspx and ascx is quite powerful, and asax is a little confused. This is not a problem for the time being. What I want to talk about today is this command.

Embarrassing
The development technology of the server, including instructions, must be a standard in web development. Asp, the predecessor of asp.net, already has this command. As the only command to reference external files, Beginners should have used it. include and require in php are almost overutilized, I have used php for a while, and this command can't be used more often. jsp has no development experience. After searching, this include command also exists, and the usage should be very common. However, the include in asp.net is almost in the forgotten corner. It is rarely mentioned in the asp.net book. Therefore, for those who have no experience in asp development and directly learn asp.net, they may have to be unaware of its existence. Why ??

One of the causes of embarrassment is one of the most important functions of webform: User Controls. The reason for using include is that the referenced content is used in many places-reuse, such as the page header, footer, and website functional block. The user control is born to this end. It is a naturally reusable feature and a powerful control event process, as mentioned in MSDN:
"Even though you can still use the # include tag (by placing the public server code, controls, or HTML tags into files to be included in other webpages) to reuse code, ASP. NET is commonly used to use Web user controls. User Controls provide an object-oriented programming model and provide more functions than files on the server ."
Microsoft never saw it. Then, include was also put into the cold room by developers. However, please do not forget that it exists.

Include details
I am not going to explain it here. For details, see: the server includes the command syntax. The syntax, usage, and examples are not mentioned here. Let's go and test it on your own.

Include advantages
Speaking of its advantages, there must be a comparison object. By the way, this object is a user control. What is a user control.

One advantage: Reuse of static files.

The user control is powerful, but even if static content is implemented in the form of a user control, it must go through the lifecycle of the user control, which is inferior to the performance.

You may say, how much static content does the website have today, such as footer, which must be managed by the CMS background of the website, how can it exist in the form of static files? Only user controls can be used, and outputcache can be added to user controls. This ensures flexibility and performance, which is the most perfect.

Yes. First, static content must be referenced in some cases when different projects have different requirements. In this case, make sure that static content exists in the form of static content instead of in the user control. Second, there is always a idea of static website optimization at any time. For example, the footer above can be generated by generating a static footer file, when CMS modifies these items, it re-generates the file and then uses the include Method for reference. Compared with the unpredictability and controllability of the cache, the advantage is more flexible than the user control, and the performance is better. Third, collaboration is for relatively large websites and some page controls, but the data is the responsibility of other teams, and some relatively stable content or content that is not sensitive to changes, static files are automatically distributed after they are generated, and references to these files are also very common.

In fact, this may also be the only include advantage.

Advantage 2: penetrating encapsulation.

Reusable items are generally encapsulated. A user control is an independent entity hosted on a page, but it is like a free kingdom on the page. When it comes to processing within the Kingdom, the page will give the processing permission to the control, for example, when page_load is loaded on a page, a control can only call the load processing method provided by the control. This is the encapsulation of the Control. Only the attributes and methods that you are willing to publish can be accessed by the page, this is a standard encapsulation, which is also determined by the page lifecycle of asp.net. Therefore, if a control wants to use a variable of a boarding page, it should publish an attribute and assign values to it by the page in the code.

Similarly, different mechanisms determine different behaviors. From MSDN:

Values assigned to the File or Virtual feature must be enclosed by quotation marks. Process included files before executing any dynamic code.

Important: process the contained files before executing any dynamic code. What does this sentence mean ??

One layer means that the content of the contained file will be added to the page after being processed. When dynamic code is executed, the executed Code cannot tell whether the content is referenced or originally exists, that is to say, we can add codes such as <% code %> or <% = page variable %> to the static files that can be reused, and they will be processed as the corresponding content by the page. This is what I call "penetrating encapsulation ". Of course, this method is more flexible than the open properties of the control, and the active page assignment.

The second layer means that if the referenced aspx or ascx is used, the page code will be executed only after the referenced dynamic content is executed and output. In this way, it is better to call the user control to put the process on the page, do not use include to reference dynamic pages.

Therefore, we should only use include to reference static files, but static files can contain server-side tag code.

I know nothing, but I have limited capabilities and I don't know what to say. You should try to see it, but it is certain that you can use include to reuse static files. There may be other scenarios that are more suitable for use and more advantages. Please also have full experience to add.

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.