数据缓存依赖、图片防盗链 url重写
数据库缓存依赖
1、数据库缓存依赖
-S服务器名称 -E集成身份验证 -ed启动 -d数据库名称 -et指定缓冲依赖的表名 -t表名
在vs2010的命令提示符中运行(切换到aspnet_regsql.exe所在的目录)
aspnet_regsql.exe -S 127.0.0.1 -E -ed -d myphotos -et -t photos
缓存依赖禁用该数据库
aspnet_regsql -S . -E -dd -d myphotos
2、依赖于数据库的web.config配置
<system.web>
<caching>
<sqlCacheDependency enabled="true">
<databases>
<add name="myphotos" connectionStringName="connectionString" pollTime="500"/>
</databases>
</sqlCacheDependency>
</caching>
3、 System.Web.Caching.SqlCacheDependency dep = new System.Web.Caching.SqlCacheDependency("myphotos", "photos");
Cache.Insert("list", list, dep);
进程外session
StateServer 对象必须可序列化
1.开始 ASP.NET 状态服务 services.msc
2.配置webconfig
<sessionState mode="StateServer" stateConnectionString="tcpip=localhost:42424"></sessionState>
//
3.如果是远程的stateserver配置注册表 regedit
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\aspnet_state\Parameters\AllowRemoteConnection
设置成1
图片防止盗链
void Application_BeginRequest(object sender, EventArgs e)
{
if (Request.RawUrl.Contains("images/"))
{
if (Request.UrlReferrer == null || !IsSameDomain(Request.UrlReferrer, Request.Url))
{
////
Response.ContentType = "image/jpeg";
string path = Request.MapPath("~/daolian.jpg");
Response.WriteFile(path);
//结束请求
Response.End();
}
}
}
//判断两个域名是否相等
bool IsSameDomain(Uri u1,Uri u2)
{
return Uri.Compare(u1, u2, UriComponents.HostAndPort, UriFormat.SafeUnescaped, StringComparison.CurrentCultureIgnoreCase) == 0 ? true : false;
}
url重写
一、原理
void Application_BeginRequest(object sender, EventArgs e)
{
//url重写
HttpApplication app = sender as HttpApplication;
string url = app.Request.RawUrl;
Regex r = new Regex("/(\\d+)/details\\.htm",RegexOptions.IgnoreCase);
Match m = r.Match(url);
if (m.Success)
{
string id = m.Groups[1].Value;
app.Context.RewritePath("~/PhotoDetails.aspx?id=" + id);
}
}
二、urlRewriter
1、在<configSections>节点加入
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
2、在</configSections>之后加入
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/(\d{4})/(\d{2})/Default\.aspx</LookFor>
<SendTo>~/Default.aspx?ID=$1</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
3、<httpHandlers>中加入
<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
或者 <httpModules>加入
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />