数据库缓存依赖
1、数据库缓存依赖
-S服务器名称 -E集成身份验证 -ed启动 -d数据库名称 -et指定缓冲依赖的表名 -t表名
在vs2010的命令提示符中运行(切换到aspnet_regsql.exe所在的目录)
aspnet_regsql -S . -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="connstr" pollTime="500"/>
</databases>
</sqlCacheDependency>
</caching>
3、 //依赖于数据库
System.Web.Caching.SqlCacheDependency dep = new System.Web.Caching.SqlCacheDependency("myphotos",
"photos");
Cache.Insert("list", list, dep, System.Web.Caching.Cache.NoAbsoluteExpiration,
System.Web.Caching.Cache.NoSlidingExpiration);
4.配置缓存
Cache.Insert("photos", list, scd);
···························
进程外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;
}
权限判断
public void Init(HttpApplication context)
{
//获得状态 AcquireRequestState
context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
}
void context_AcquireRequestState(object sender, EventArgs e)
{
//验证权限
HttpApplication app = sender as HttpApplication;
if (app != null)
{
if (!app.Request.RawUrl.ToLower().Contains("login.aspx"))
{
if (app.Session["user"] == null)
{
app.Response.Write("<script>alert('没有权
限');window.location.href='Login.aspx?returnurl="+app.Request.RawUrl+"'</script>");
app.Response.End();
}
}
}
}
<!--自定义错误页-->
<customErrors mode="RemoteOnly" defaultRedirect="error.html" >
<error statusCode="403" redirect="nosuccess.htm"/>
<error statusCode="404" redirect="404.htm"/>
</customErrors>
url重写
<system.webServer>
<modules>
<add name="urlrewriter" type="WebForm.UrlRewrite"/>
</modules>
</system.webServer>
一、原理
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" />
4.。。。。。。。。。。。。。
HttpRuntime--->创建了HttpContext HttpApplication--->
执行HttpApplication的19个事件----->通过httpmodule注册19个事件中的某些 对请求进行过滤--->请求最终到
达httphandler 执行它的ProcessRequest(HttpContext hc)
第8个事件创建了httphandler 的对象
第11和12个事件之间执行pr方法
域名 空间
先安装iis 再安装fw
先安装的fw 再安装的iis 重新在iis中注册fw
打开vs2010的命令提示符 aspnet_regiis -i
asp.net缓存和配置信息防盗链,自定义错误页
最新推荐文章于 2018-06-21 16:20:34 发布