ASP.net Substitution 页面缓存而部分不缓存的实现方法
在ASP.NET中要实现部分内容非缓存,而其它的都需要缓存输出,可以使用Substitution控件实现.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ outputcache duration="10" varybyparam="none" %>
<!--设置缓存时间为10秒-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head id="Head1" runat="server">
<title>Substitution控件实例</title>
</head>
<body>
<form id="Form1" runat="server">
<h1>Substitution控件实例演示</h1>
您好<b>
<asp:substitution id="m1" methodname="GetUser" runat="Server">
</asp:substitution></b><br>
当前时间[非缓存]:
<b><asp:Substitution ID="m2" runat="server" MethodName="GetTime" /></b>
<br />
缓存: <%=DateTime.Now.ToLongTimeString() %>
<asp:Button ID="Button1" runat="server" Text="刷新" />
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
//下面定义了两个表态方法,可以使用Substitution控件调用,以实现这两个方法每次刷新时都要调用
public static string GetUser(HttpContext context)
{
return context.User.Identity.Name;
}
public static String GetTime(HttpContext context)
{
return DateTime.Now.ToString();
}
}