ASP.NET的页面中对其他文件的引用

先来看看ASP风格的

===============

你可以把一个ASP页面的内容在服务器执行之前添加到另外一个ASP页面中, 方式是使用#include命令.

#include命令被用来创建函数, headers, footers, 或者将要被多个页面重用的部分.

如何使用?

这是"mypage.asp"

<html>
<body>
    <h3>
        Words of Wisdom:</h3>
    <p>
        <!--#include file="wisdom.inc"-->
    </p>
    <h3>
        The time is:</h3>
    <p>
        <!--#include file="time.inc"-->
    </p>
</body>
</html>

这是"wisdom.inc"文件

"One should never increase, beyond what is necessary,
the number of entities required to explain anything."

这是"time.inc"文件

<%
Response.Write(Time)
%>

如果你在浏览器里查看源代码, 你会看到这样:

<html>
    <body>
        <h3>
            Words of Wisdom:</h3>
        <p>
            "One should never increase, beyond what is necessary, the number of entities required
            to explain anything."</p>
        <h3>
            The time is:</h3>
        <p>
            11:33:42 AM</p>
    </body>
</html>

语法

<!--#include virtual="somefilename"-->
这里的virtual表明地址是起自一个虚拟路径. 比如说, 一个名叫header.inc的文件存在于一个叫做/html的虚拟目录下, 那么下面的一段命令会插入header.inc的内容

<!-- #include virtual ="/html/header.inc" -->


<!--#include file ="somefilename"-->

这里的file表明路径是一个相对路径. 如果有一个文件在html文件夹下, 并且header.inc文件存在于html\headers中, 那么下面的代码会插入header.inc文件的内容

<!-- #include file ="headers\header.inc" -->

在ASP.NET页面中动态包括HTML文件和客户段脚本文件

===========================

因为ASP.NET应用程序在发送给客户端之前要经过编译, 运行的, 所以在服务器端include文件的时候, 你不能使用变量来替代文件名(比如说:<!-- #include PathType = FileName –>" ). 然而, 你可以使用Response或者StreamReader对象来向HTML流中写入被include的文件.

举例:

   <%@ Page Language="vb" AutoEventWireup="false"%>
   <html>
   <body>
        <%           
          Response.WriteFile ("Yourfile.inc")
        %>
   </body>
   </html>

在ASP.NET页面中插入指定文件的内容, 包括Web pages(.aspx文件), user control files(.ascs文件), 还有Global.asax文件

============================

语法, 与ASP风格的include 相同.

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

Remarks

赋予File或者Virtual属性的值必须被双引号括起来(""). 被included的文件会在任何动态代码执行前被处理. Include文件能被用来包含从静态文本(比如说普通的页面header或者公司地址), 到包含土工的服务器端代码(server-side code), 控件(control), 或者开发者想要插入到其他页面中的HTML标签块.

尽管你也可以使用这个#include 的方式来重用代码, 在ASP.NET中通常的更好的方式是使用Web user controls. 因为user control提供了面向对象的编程模型, 而且比服务器端的include功能更加强大.

#include标签必须被HTML或XML的注释边界符括起来, 来避免它被解释成为字面上的文本.

举例:

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

参考资料:

How To Dynamically Include Files in ASP.NET

http://support.microsoft.com/kb/306575

Server-Side Include Directive Syntax

http://msdn.microsoft.com/en-us/library/3207d0e3.aspx

ASP Including Files

http://www.w3schools.com/asp/asp_incfiles.asp

原文地址:https://www.cnblogs.com/awpatp/p/1665573.html