您的位置:首页 > 其它

.net页面缓存实验笔记

2012-01-03 17:25 253 查看
在项目中新建一个website,新建一个PageCache.aspx页面,在页面代码的第二行填写:

<%@ OutputCache Duration="3" VaryByParam="none" %> //不根据任何URL的参数进行缓存



<%@ OutputCache Duration="3" VaryByParam="id" %> //根据URL的参数id进行缓存

PageCache.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageCache.aspx.cs" Inherits="PageCache" %>
<%@ OutputCache CacheProfile="myPageCache" VaryByParam="id" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:Button ID="btnShowTime" runat="server" onclick="btnShowTime_Click"
Text="ShowTime" />
<asp:TextBox ID="txtShowTime" runat="server"></asp:TextBox>
<br />
<asp:Label ID="lbShowTime" runat="server"></asp:Label>

<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="VaryByParam=id" />
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:DropDownList>

</div>
</form>
</body>
</html>


PageCache.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class PageCache : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ShowTime();
}
}

protected void btnShowTime_Click(object sender, EventArgs e)
{
txtShowTime.Text = System.DateTime.Now.ToString();

}
protected void ShowTime()
{
lbShowTime.Text = System.DateTime.Now.ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Pagecache.aspx?id=" + DropDownList1.SelectedValue);
}
}


Web.config:(缓存设置代码见
<caching>
段落)

<?xml version="1.0"?>

<!--
For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 -->

<configuration>
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>

<system.web>
<compilation debug="false" targetFramework="4.0" />

<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880" />
</authentication>

<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
applicationName="/" />
</providers>
</membership>

<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
</providers>
</profile>

<roleManager enabled="false">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
<add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
</providers>
</roleManager>

<caching>
<outputCache enableOutputCache="true">
</outputCache>
<outputCacheSettings>
<outputCacheProfiles>
<add name="myPageCache" duration="10" enabled="true"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>

</system.web>

<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

</configuration>


在IE中运行PageCache.aspx,点击页面上的"ShowTime”按钮,每十秒钟更新一次时间值。

点击"VaryByParam=id"按钮, 每10秒更新一次URL中ID参数=下拉菜单中选中值的页面的时间值

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: