Use and advantages of asp. net server side instruction include

  • 2020-06-03 06:20:12
  • OfStack

asp.net server includes instruction the simple point is 1 < ! - # include file | virtual = "filename" � > msdn inserts the contents of the specified file into the ES10en.NET file, including web pages (.aspx), user controls (.ascx), and Global.asax files. Insert static file this basic function will not say, insert aspx, ascx, this function is quite strong, asax brother is a little confused, this leave aside, today is going to talk about this directive.

Embarrassing presence
Server-side including instructions is certainly standard in web development techniques. asp. net's predecessor, asp, already has this directive, and as the only 11 that refer to external files, it should be used by beginning developers; include and require of php are almost overused. A paragraph of php has been used, and the one commonly used can no longer be used. jsp has no experience in development. After searching, there is this include instruction, and its usage should be very common. But include in ES32en.net is almost in a forgotten corner, rarely mentioned in the books read by asp.net. Therefore, those who have not learned asp.net directly from asp development experience may not even know it exists. Why??

One reason for the embarrassment is the most important feature in webform: user controls. The reason for using include is that referenced content is used in many ways -- for reuse, such as headers, footers, and web site function blocks. And that's exactly what user controls are made for, with their inherently reusable features and powerful control event flow, as described in MSDN:
"While you can still use the #include tag for code reuse (by putting common server-side code, controls, or HTML tags into files to be included on other web pages), the preferred method often used by ES48en.NET is to use Web user controls. User controls provide an object-oriented programming model and provide more functionality than server-side include files.
Microsoft turned its back on it, and include fell out of favor with developers, but don't forget, it exists.

include,
The server side includes the instruction syntax. Grammar, usage, examples and other things, here also do not say, you go to see or their own experiment.

include advantage
Speaking of advantages, there is definitely an object to compare, and that object is a user control. User control is what thing, do not explain.

Advantage 1: Reuse of static files.

User controls are powerful, but even something as simple as static content implemented as a user control must go through the user control's life cycle, which is poor compared to performance.

You might say, site and how many can now is pure static content, such as the footer, sure, those items are CMS website backstage management, how may exist in the form of static files, can only use the user control, and give the user control plus outputcache, which guarantees the flexibility, performance can be guaranteed, this is the most perfect.

Sure, number one, different projects have different requirements, and sometimes they will refer to static content. Make sure that you use that static content in a static form, not in a user control. 2, website optimization, whenever possible, there is always one kind of point of view, is static, such as the above the footer, we can generate a static page script file, when CMS is modified for these items to regenerate this file, and then by include reference, relative to the cache unpredictability and 1 set is not controllable, flexible advantage than user controls, and better performance. Third, collaboration, is for relatively large sites, page some controls, but the data is the responsibility of other teams, and 1 some relatively stable content or less sensitive to change content, generated static files after automatic distribution, and the reference to these files is also very common.

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

Advantage 2: Penetrable encapsulation.

Reusable things are generally encapsulated. User controls is an independent individual, it is hosted by the page, but itself is like a free kingdom on a page, run into the kingdom of the internal processing, page will give the discretion to control their own to deal with, such as page page_load loads, touch control can only call control provide load processing method, which is the control of encapsulation, only themselves willing to public properties and methods can be page access, this is a standard packaging, this is also the asp. net decided the page life cycle. Therefore, if the control is going to use a variable from the host page, it should expose a property and then have the page actively assign a value to it in the code.

Again, different mechanisms determine different behaviors. From MSDN:

The value assigned to File or Virtual must be enclosed in quotes (""). Process the contained file before executing any dynamic code.

Important 1. Process the contained file before executing any dynamic code. What does this sentence mean?

Layer 1 means that the content is not added to the page until the included file has been processed. When dynamic code is executed, the executed code does not distinguish whether the content is a reference or its own, that is, it is added to a static file that can be reused < % code % > or < %= page variable % > Such code will be processed into the corresponding content by the page. This is what I call "penetrable encapsulation". This approach is certainly more flexible than controls that expose properties and actively assign values to pages.

If you refer to aspx or ascx, then the page code will execute after the output of the referenced dynamic content, which is not as good as a user control call to keep the flow inside the page. Try not to use include to refer to dynamic pages.

So we should only use include to refer to static files, but static files can contain server-side markup code.

You can tell everything you know, but your ability is limited and words fail to convey your meaning. Look at it as much as you can, but you can certainly reuse it as a static file and use include. There may be other more suitable usage scenarios and more advantages, please have full experience to supplement.


Related articles: