Freigeben über


Server-Side Include Directive Syntax

Inserts the contents of a specified file within an ASP.NET file, including Web pages (.aspx files), user control files (.ascx files), and Global.asax files..

<!-- #include file|virtual="filename" -->

Attributes

  • File
    The file name is a physical path from the directory containing the file with the #include directive. This path can be relative.

    Note

    The included file can be in the same directory or in a subdirectory; it cannot be in a directory above the file with the #include directive.

  • Virtual
    The file name is a virtual path from a virtual directory in your Web site. This path can be relative.

    Note

    This technique is recommended because of potential changes in the physical path to a file.

Remarks

The value assigned to the File or Virtual attribute must be enclosed in quotation marks (""). The included file is processed before any dynamic code is executed. Include files can be used to contain anything from static text (such as a common page header or a company address) to common server-side code, controls, or blocks of HTML markup that a developer wants to insert within other pages.

Note

Although you can still use the #include tag for the purpose of code reuse (by placing common server-side code, controls, or HTML markup within a file to include in other Web pages), often the preferred approach in ASP.NET is to use Web user controls. User controls provide an object-oriented programming model and much greater functionality than server-side includes. For details, see ASP.NET User Controls.

The #include tag must be enclosed within HTML or XML comment delimiters to avoid being interpreted as literal text.

Example

The following code example demonstrates how you can use server-side include directive syntax to call files that will create a header and a footer on an ASP.NET page. Both are using relative paths.

<html>
   <body>
      <!-- #Include virtual="/include/header.inc" -->
        Here is the main body of the .aspx file.
      <!-- #Include virtual="/include/footer.inc" -->
   </body>
</html>

See Also

Concepts

ASP.NET Web Page Syntax Overview

Other Resources

ASP.NET User Controls