ASP 缓存处理及URL 重写
1 缓存
1.1.1
<%--通过设置VaryByParam ="none" 来实现 整页缓存 --%>
<%@ OutputCache Duration="" VaryByParam ="none" %>
1.1.2
<%--带参数缓存,只要包含在VaryByParam 中的任何一个参数改变都会使页面缓存失效,如果当前参数不包含在VaryByParam中,则改变也无效--%>
<%@ OutputCache Duration="" VaryByParam="id;name;age" %>
1.1.3
<%--根据控件id来缓存整个页面--%> <%@ OutputCache Duration="" VaryByControl="a" %>
1.2 【Wed.config】 配置 缓存
<%@ OutputCache CacheProfile="MyCacheTest"%> 【Wed.config】 下的配置
<system.web>
<compilation debug="true" targetFramework="4.0" />
<!--统一页面缓存管理配置节点-->
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<!--duration:缓存事时间 name:缓存名称,在页面使用的时候 CacheProfile要指定name中设置的值-->
<add name="MyCacheTest" duration="" varyByParam="none" />
</outputCacheProfiles>
</outputCacheSettings>
</caching>
1.3 文件缓存 在asp中的后台代码:
protected void Page_Load(object sender, EventArgs e)
{
string atxtContent; if (Cache["datetimenow"] != null)
{
//lbdatetime.Text = Cache["datetimenow"].ToString();
lbdatetime.Text = Cache["datetimenow"].ToString();
}
else
{
DateTime now = DateTime.Now;
#region 设置缓存的绝对过期时间
Cache["datetimenow"] = now.ToString();
//设置缓存的绝对过期时间 中不设置相对过期时间 写法一:TimeSpan.Zero
Cache.Insert("datetimenow", now, null, DateTime.Now.AddSeconds(), TimeSpan.Zero);
//设置缓存的绝对过期时间 中不设置相对过期时间 写法二:System.Web.Caching.Cache.NoSlidingExpiration
Cache.Insert("datetimenow", now, null, DateTime.Now.AddSeconds(), System.Web.Caching.Cache.NoSlidingExpiration);
#endregion #region 设置缓存的相对过期时间
//设置缓存的相对过期时间
Cache.Insert("datetimenow", now, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(, , , )
, System.Web.Caching.CacheItemPriority.High, myCacheItemRemovedCallback);
#endregion #region 文件缓存依赖,首先:绝对过期时间和相对过期时间要设置不过期
//文件缓存依赖,首先:绝对过期时间和相对过期时间要设置不过期
// 定义该缓存要依赖的文件,注意传入文件的绝对路径 atxtContent = System.IO.File.ReadAllText(Server.MapPath("b.txt"));
CacheDependency cdFile = new CacheDependency(Server.MapPath("a.txt")); Cache.Insert("datetimenow"
, atxtContent
, cdFile
, System.Web.Caching.Cache.NoAbsoluteExpiration
, System.Web.Caching.Cache.NoSlidingExpiration
, System.Web.Caching.CacheItemPriority.High, myCacheItemRemovedCallback
);
#endregion lbdatetime.Text = atxtContent;
} } #region 当缓存被移除的时候,系统会自动调用该回调函数 +myCacheItemRemovedCallback(string key, object value, CacheItemRemovedReason reason)
/// <summary>
///当缓存被移除的时候,系统会自动调用该回调函数
/// </summary>
/// <param name="key">当前失效的缓存key</param>
/// <param name="value">当前失效时缓存key对应的缓存内容</param>
/// <param name="reason">缓存失效的原因</param>
void myCacheItemRemovedCallback(string key, object value, CacheItemRemovedReason reason)
{
System.IO.File.WriteAllText(@"E:\传智播客\7期班\2013-9-15 asp.net高级 缓存,工厂,单例\源代码\缓存\WebApplication1\log.txt"
, "key=" + key + " value=" + value + " reason=" + reason);
}
#endregion
1.4 数据库缓存 直接在【Web.config】中配置
<configuration>
<connectionStrings>
<add name="CachedbConnectString" connectionString="data source=.;initial catalog=PhoneBook;user id=sa;password=123" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<!--统一页面缓存管理配置节点-->
<caching> <sqlCacheDependency enabled="true" pollTime="">
<databases>
<add name="PhoneBook" connectionStringName="CachedbConnectString"/>
</databases>
</sqlCacheDependency> </caching> </system.web> </configuration>
URL重写
方法一:
新建一个 【Global.asax】文件 在它的 Application_BeginRequest 方法里写 获取URL,并通过 正则表达式 将 URL 重写
//得到当前url的请求路径
string currUrl = HttpContext.Current.Request.RawUrl; //top/1
string newUrl = ""; //定义正则表达式
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("index/(.*)/(.*)");
if (reg.IsMatch(currUrl))
{
//将匹配的url替换成"index.aspx?id=$1&name=$2" 类型的字符串
newUrl = reg.Replace(currUrl, "index.aspx?id=$1&name=$2"); //重写是靠RewritePath(path) path只能传相对路径index.aspx?id=1&name=2 而不能传http://loclhost/index.aspx?id=1&name=2
HttpContext.Current.RewritePath(newUrl);
方法二:在Internet 中选中网站后
在URL 重写中 --添加规则 --用户友好URL


在这里就是将你要修改的URL写到URL 输入框里,下面的下拉框选择你要的URL格式

如果有漏的,希望有大神补充一下!!
ASP 缓存处理及URL 重写的更多相关文章
- 转载MSDN 在ASP.NET 中执行 URL 重写
转载文章原网址 http://msdn.microsoft.com/zh-cn/library/ms972974.aspx 摘要:介绍如何使用 Microsoft ASP.NET 执行动态 URL 重 ...
- ASP.NET伪静态 UrlRewrite(Url重写) 实现和配置
核心提示:大家一定经常在网络上看到很多网站的地址后缀都是用XX.HTML或者XX.ASPX等类似静态文件的标示来操作的吧,那么大家有怀疑过他真的是一个一个的静态生成的文件么,静态文件的生成的优缺有好有 ...
- 【ASP.NET Core】URL重写
今天老周和大伙伴们聊聊有关 Url Rewrite 的事情,翻译过来就是 URL 重写. 这里不得不提一下,URL重定向与重写的不同. 1.URL重定向是客户端(通常是浏览器)向服务器请求地址A,然后 ...
- 在 ASP.NET 中执行 URL 重写的方法
由于需要我们经常会想将动态的页面做成伪静态的,或者在get传值的时候使其简洁明了(实现“可删节”的URL),这时我们需要用到URL重写,微软的MSDN上有详细的原理和使用介绍.我这里就将一种简单的使用 ...
- ASP.NET过滤器、URL重写
过滤器可以对指定的URL访问进行拦截,并执行过滤器的方法,根据实际应用情况,在过滤器中修改请求的代码.判断会话信息,也可以做权限控制. 一:编写过滤器,实现URL重写并配置过滤 编写过滤器,就是编写一 ...
- ASP.NET 中执行 URL 重写
具体实现步骤(其中的一种实现方法): 一.下载相关的DLL(ActionlessForm.dll和UrlRewriter.dll) http://download.csdn.net/detail/yi ...
- UrlRewrite(URL重写)--ASP.NET中的实现
概述 今天看了下URL重写的实现,主要看的是MS 的URL Rewrite. URL重写的优点有:更友好的URL,支持老版本的URL URL重写的缺点有:最主要的缺点是性能低下,因为如果要支持无后缀的 ...
- ASP.NET Core 2 学习笔记(八)URL重写
路由跟URL 重写的功能性略有不同.路由是将Request 找到对应的服务,而URL 重写是为了推卸责任转送Request.本篇将简单介绍下ASP.NET Core的URL重写(URL Rewrite ...
- C# WebService URL重写
背景 有时候我们会有这样的需求,将 WebService URL 中的 asmx 后缀去掉:或者我们要模拟普通 Web 的 URL,接口名称直接拼接在 URL 中.这些情况我们都要用到URL重写. 关 ...
随机推荐
- web.xml执行顺序
出自:http://blog.csdn.net/u010833154/article/details/50697987 引言:启动java web程序的时候,java web会读取配置文件web.xm ...
- Qt(自适应窗口)
关于窗口布局: 默认控件的大小为最小尺寸: 如果此时右键设置布局时,窗口大小会自动缩放对应大小,不利于调整. 建议窗口控件设置好最小尺寸,便于窗口布局时,控件不会变形,例如: 改变minimumsiz ...
- js生成元素的事件不执行问题
要求:双击span标签,可以修改.<div class="commands_ticketmiddl_ine_new" > <span>NO:</spa ...
- 移动端web页面input限制只能输入数字
<input type="number" pattern="[0-9]*" /> 如上所示,在安卓端设置input类型为number,可限制键盘只输 ...
- go cobra
https://github.com/spf13/cobra https://github.com/spf13/cobra/blob/master/bash_completions.md go get ...
- Linux实战教学笔记33:lvs+keepalived集群架构服务
一,LVS功能详解 1.1 LVS(Linux Virtual Server)介绍 LVS是Linux Virtual Server 的简写(也叫做IPVS),意即Linux虚拟服务器,是一个虚拟的服 ...
- Nginx实现ssl一级、二级域名证书部署并用https访问代理转发服务器
1. 规划 域名 解析IP Nginx代理 htpps://www.devcult.com 47.88.10.155 htpps://auto.devcult.com 47.88.10.155 ...
- 10 华电内部文档搜索系统 search04
上一节我们着重讲解了权限过滤器的使用.这一节讲解一下补充一下Struts 2下面对应的Checkbox的使用.对索引下面的数据进行维护操作,一次可以删除多个.在Struts 2下面使用对应的Check ...
- 仿微信-ActionSheet
有时候我们在开发中,系统提供的actionsheet 不能满足我们的需求,所以,今天就做一个类似微信中的,支持多个按钮,我见有的人用的是个tableview,也可以,但是有点麻烦. 效果图: Acti ...
- 使用jquery修改表单的提交地址
基本思路: 通过使用jquery选择器得到对应表单的jquery对象,然后使用attr方法修改对应的action 示例程序一: 默认情况下,该表单会提交到page_one.html 点击button之 ...