asp.NET中使用include

asp.net|include

A common way to build the navigation and layout for an ASP-driven website is to use include files. Most advanced ASP developers know that when you do this, it is best to encapsulate the functionality of the include file in a Sub or Function, and then to call this routine from the page that is including the file. This avoids problems with variable scope, allows parameters to be passed easily to the include file, and makes the code easier to read.

As these sites are migrated to use ASP.NET, it is likely that classic ASP and ASP.NET pages will exist side-by-side, which is one of the touted advantages of ASP.NET. Unfortunately, there is no built-in way for an ASP.NET page to take advantage of a classic ASP include file, which means that the obvious solution if you want to maintain a consistent look and feel is to duplicate the look of the classic ASP template in an ASP.NET user control. Unfortunately, this means duplicating presentation logic, and inevitably, the classic ASP template and the ASP.NET template will get out of sync.

To overcome this problem, I built a simple user control that uses ASP.NET's built-in page-scraping library, HTTPWebResponse, to grab a template ASP file and render it in my .aspx page. The template ASP file is simply a page that includes my presentation logic include file, and calls the functions to render the HTML, passing through any querystring parameters it has to those functions (such as for page title for a header include file).

For this demonstration, there are six files:

layout_sample.asp - this ASP page uses the include file the standard Classic ASP way.
header_include.asp - this is my actual ASP include file, which has a function called showHeader that will display the HTML for the page header wherever it is called. The page header is just an HTML table with the title of the page in it. The title is passed into showHeader as a required parameter.
header_template.asp - this is my template file. All it does is include my ASP header include file, call the showHeader function, and insert the querystring parameter for the title. If you click on this page, add "?title=foo" to the url to see how it uses the title from the querystring.
showHeader.ascx -- My user control that scrapes an ASP page to get the HTML to insert in my .aspx page.
showHeader.ascx.cs -- The code-behind file for my user control.
layout_sample.aspx - this ASP.NET page will use the Classic ASP layout

The Classic ASP example is very simple. All it does is include a file, call showHeader, and wrap it all in a basic HTML page:

1 <%Option Explicit%>
2 <!-- #INCLUDE FILE="header_include.asp" -->
3 <%
4 'Declare Variables
5 Dim title
6
7 title = "Sample Layout"
8 %>
9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
10 "http://www.w3.org/TR/REC-html40/loose.dtd">
11 <html>
12 <head>
13 <title><%=title%></title>
14 </head>
15 <body>
16 <% Call showHeader(title) %>
17 <p>
18 This is the main content of my sample Classic ASP page. Compare it to the
19 <a href="layout_sample.aspx">ASP.NET version</a>.
20 </p>
21 </body>
22 </html>

The include file is equally simple -- a single method that outputs some HTML. Note that this file can be called by either VBScript or JScript ASP pages, and avoids any context switching. It could also be called several times on one page, if need be.

1 <script runat="server" language="vbscript">
2 Sub showHeader(title)
3 Response.Write "<table width=""100%"" bgcolor=""#CC0000"" border=""1"">"
4 Response.Write "<tr><td align=""center""><b>" & title & "</b></tr></td>"
5 Response.Write "</table>"
6 End Sub
7 </script>

Now let's take a look at the real "hack" part of this implementation, the dummy ASP page that is nothing more than a page that calls our include file (header_template.asp):

1 <%Option Explicit%>
2 <!-- #INCLUDE FILE="header_include.asp" -->
3 <% Call showHeader(Request("title")) %>

Finally, we can look at the user control that makes this whole thing work. It's pretty simple. All it does is use the HTTPRequest object that is built into ASP.NET to grab the header_template.asp page and insert it into the ASP.NET page. Something similar could be done with the ASPHTTP object in Classic ASP. Here's the file:

showHeader.ascx
1 <%@ Control language="c#" src="showHeader.ascx.cs"
2 Inherits="ASPAlliance.UserControls.showHeader" %>
3 <%@ OutputCache Duration="3600" VaryByParam="none" %>
4 <!-- Header Cached: <%=System.DateTime.Now%> -->
5 <asp:literal id="header" runat="server"/>
showHeader.ascx.cs
1 namespace ASPAlliance.UserControls
2 {
3 using System;
4 using System.IO;
5 using System.Net;
6 using System.Text.RegularExpressions;
7 using System.Web;
8 using System.Web.UI;
9 using System.Web.UI.WebControls;
10
11 public abstract class showHeader : System.Web.UI.UserControl
12 {
13 // Declare Controls
14 protected System.Web.UI.WebControls.Literal header;
15 public String title = "";
16
17 public showHeader(){
18 this.EnableViewState = false;
19 }
20
21 private void Page_Load(object sender, System.EventArgs e)
22 {
23 header.Text = readHtmlPage ("http://www.aspalliance.com/stevesmith/articles/examples/includelets/header_template.asp?title=" +
24 title + "&" +
25 Request.ServerVariables["QUERY_STRING"]);
26 header.Text = Regex.Replace(header.Text,
27 "</title>",
28 title + "</title>");
29 header.Text = Regex.Replace(header.Text,
30 "/libraryaspa/SSheader.asp",
31 Request.ServerVariables["URL"]);
32 }
33
34 private String readHtmlPage(string url)
35 {
36 WebResponse objResponse;
37 WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
38 objResponse = objRequest.GetResponse();
39 StreamReader sr = new StreamReader(objResponse.GetResponseStream());
40 return sr.ReadToEnd();
41 }
42 }
43 }
44
45
46

This is really pretty straightforward. In the page_load of the control, we grab use the HTTPWebResponse object to scrape the contents of header_template.asp, passing it our public property, page_title. We then suck in the result and display it in an asp:label tag. You might think this would be just atrociously slow, but in practice it works reasonably well. Throw a page-level output cache on your .aspx page, and any performance problems you might encounter disappear anyway. The only issue I've run into thus far is that sometimes images just don't show up through the web-scraper. This usually happens with larger images, so I think it has something to do with the scraper (HTTPWebResponse) object running out of time before it needs to return.

To conclude this example, let's take a look at one last page, the ASP.NET file that uses this control:

1 <%@ Page Language="C#" Trace="False" %>
2 <%@ Register TagPrefix="SSTemplate" tagname="showHeader" src="showHeader.ascx"%>
3 <%@ OutputCache Duration="100" VaryByParam="*" %>
4 <script runat="server">
5 String page_title = "Sample ASP.NET Layout";
6 void Page_Load(Object Src, EventArgs E){
7 // you can set the title here programatically
8 header.title = page_title;
9 // or down below we could declaratively add title="Sample ASP.NET Layout" to our tag.
10 }
11 </script>
12 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
13 "http://www.w3.org/TR/REC-html40/loose.dtd">
14 <html>
15 <head>
16 <title><%=page_title%></title>
17 </head>
18 <body>
19 <SSTemplate:showHeader id="header" runat="server" />
20 <p>
21 <p>
22 This is the main content of my sample ASP.NET page. Compare it to the
23 <a href="layout_sample.asp">Classic ASP version</a>.
24 </p>
25 </p>
26 </body>
27 </html>

This page is written in C#, just because I can, and also demonstrates how ASP.NET allows for easy code interoperability. In this case I have a C# page invoking a VB.NET user control. In fact, I'm even using a Classic ASP VBScript method via an ASP include file, although somewhat indirectly!

By using this user control, you can maintain a single location for your site's layout templates, rather than having to maintain two sets of layout files. Once all of your .asp files are converted to .aspx files, your controls are already in place and you can simply delete your .asp templates and includes and move the layout HTML into your controls directly. Hope this helps!

时间: 2024-09-12 11:03:57

asp.NET中使用include的相关文章

ASP中使用INCLUDE对搜索引擎收录是否有影响

include|搜索引擎 点石有会员提出一个问题:ASP中过多使用如<!–#include file="include/footer.asp"–>对引擎收录有影响吗?这个问题引起大家的热门讨论,zac指出include应该是服务器端执行的,SE根本不知道是include的,所以对搜索引擎没有影响.seo123说使用<!–#include file="include/footer.asp"–>对搜索引擎几乎没影响,而使用<!–#inclu

asp.net服务器端指令include的使用及优势介绍_实用技巧

      asp.net中的服务端包括指令简单点就是一个<!-- #include file|virtual="filename" –>这样的指令,msdn中的名词解释是:将指定文件的内容插入 ASP.NET 文件中,包括网页(.aspx 文件).用户控件文件(.ascx 文件)和 Global.asax 文件.插入静态文件这个基本功能就不说了,插入aspx.ascx,这功能算是挺强了,asax哥就有点困惑了,这个暂且不管,今天要说的就是这个指令. 尴尬的存在     服

ASP中处理#include (我不知道有没有人贴过,如果有的话,不要骂我)我也没有仔细看

include 813     在ASP中处理#include 文件与用编译高级编程语言,如C/C++处理包含文件,这两种方法之间有两个主要区别.第一,ASP不从最终形成的ASP文件中移走那些未涉及到的信息.这是因为ASP独立于脚本引擎,不过多地进行代码分析.大体说来,如果遇到了ASP文件的基本语法请求,信息就被缓存(假定缓冲器是打开的)并被发送到适当的脚本引擎,进行进一步的分解.标记及执行.     除了这个"死码"问题,ASP包含文件与编译语言之间的另一个区别在于:每个ASP文件都

ASP中处置#include

ASP中处理#include ASP中处理#include 在ASP中处理#include 文件与用编译高级编程语言,如C/C++处理包含文件,这两种方法之间有两个主要区别.  第一,ASP不从最终形成的ASP文件中移走那些未涉及到的信息.这是因为ASP独立于脚本引擎,不过多地进行代码  分析.大体说来,如果遇到了ASP文件的基本语法请求,信息就被缓存(假定缓冲器是打开的)并被发送到适当  的脚本引擎,进行进一步的分解.标记及执行. 除了这个"死码"问题,ASP包含文件与编译语言之间的

在ASP应用中验证用户身份

    交互式的Web应用比那些只提供静态Web页面的站点要求考虑更多的安全问题.注册与密码是保护敏感信息最为常用的手段.由于ASP没有直接提供验证用户身份的方法,因此,用户必须执行登录过程以便应用系统保存和提取用户相关信息.    一.示例站点概貌    本文通过一个示例站点ASPSecurity说明ASP应用中注册与密码保护的一般实现过程.我们把用户信息保存在Web服务器上的一个Access97数据库中,DSN名称为   ASPSecurity.唯一的数据库表拥有以下字段:Signon(文本

浅谈ASP程序设计中数据库文件调用的捷径

程序|设计|数据|数据库 引言 本文针对ASP程序设计中最基础.也是最关键的部分"数据库文件的调用"进行说明,同时谈谈ASP程序设计中数据库文件调用的一些技巧. ASP简介 ASP(Active Server Pages)是微软于1996年推出的Web应用程序开发技术,它是一种脚本语言.ActiveX组件及HTML语言等的综合,微软把它描述为"一个服务器的脚本环境,在这里可以生成和运行动态的.交互的.高性能的Web服务器应用程序".其主要功能是为生成动态的.交互式的

ASP开发中数据库文件调用

数据|数据库 引言    本文针对ASP程序设计中最基础.也是最关键的部分"数据库文件的调用"进行说明,同时谈谈ASP程序设计中数据库文件调用的一些技巧.    ASP简介    ASP(Active Server Pages)是微软于1996年推出的Web应用程序开发技术,它是一种脚本语言.ActiveX组件及HTML语言等的综合,微软把它描述为"一个服务器的脚本环境,在这里可以生成和运行动态的.交互的.高性能的Web服务器应用程序".其主要功能是为生成动态的.交

Asp.net中创建和使用Ado.net

ado|asp.net|创建     在商业应用程序中最重要的组件是数据,无论是在线的商务应用程序.公司的企业软件还是小型公司的会计应用程序无不如此:通过一个通用的线程与数据打交道,即都必须实现快速.有效.可靠的方式存储.检索和处理数据.      然而,一直一来令人棘手的问题是这些数据文件常以不同的格式存储,这就需要开发者学会用多种不同的方式来处理完全一样的事情.Microsoft等诸多数据提供者力求实现数据访问格式的标准化.      从Odbc的出现到Dao.Rdo.Oledb.Ado的实

ASP项目中的公共翻页模块

翻页|项目 在大型的ASP项目中,很多的页面都涉及到翻页功能.如果每个页面都写一个翻页的程序的话,这样的工作即降低了工作效率,也不利于工程的模块化,不能使代码重用.因此,把翻页这样的功能模块化是很有必要的.设计方法: 1.调用该模块时,只需要传递记录集和每页显示的记录的条数: 2.可以点击链接进行翻页,也可以直接输入页码,回车后翻页: 3.不要考虑文件名,程序的每次翻页都能在当前页面. 想清楚了上面3个问题,我们的公共翻页模块就可以动手了. <% '+++++++++++++++++++++++