ASP.NET08

本文总阅读量:
  1. 1. 配置错误页
  2. 2. 缓存
    1. 2.1. 创建缓存
    2. 2.2. 数据库依赖
  3. 3. HttpModule

配置错误页

1
2
3
4
5
6
7
8
9
10
11
<customErrors mode="On" defaultRedirect="~/test2/404.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
---------------------------在Application_Error中发生错误时转向另外一个页面------------------------
protected void Application_Error(object sender, EventArgs e)
{
string errorMsg = Context.Error.Message + Environment.NewLine + Context.Error.StackTrace + "【" + DateTime.Now.ToString() + "】" + Environment.NewLine + "====================================================" + Environment.NewLine;
File.AppendAllText(Server.MapPath("~/log.txt"), errorMsg);
Response.Redirect("~/test2/404.htm");
}

mvc中只能将静态页面放在根目录或者创建一个Error控制器

缓存

https://luox78.github.io/2018/02/01/cache/

分布式:一个业务分拆多个子业务,部署在不同的服务器上(redis缓存)

集群:同一个业务,部署在多个服务器上

创建缓存

  • Cache[“key”]=value;

  • Cache.Insert();

    1
    public void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback);

    dependencies:缓存依赖,一旦依赖的东西改变缓存失效

    absoluteExpiration:绝对过期时间

    slidingExpiration:滑动过期时间(相对每次使用之后)

    priority:被移除优先级

    onRemoveCallback:被移除回调函数

文件依赖例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public ActionResult Test4()
{
if(HttpRuntime.Cache["Time"] == null)
{
string time = GetTime();
HttpRuntime.Cache.Insert("Time", time,new CacheDependency(Server.MapPath("~/Views/Cache/Cache.txt")));
return View(model:time);
}
else
{
return View(model:HttpRuntime.Cache["Time"]);
}
}

//模拟耗时请求
public string GetTime()
{
System.Threading.Thread.Sleep(3000);
return DateTime.Now.ToString();
}
1
HttpRuntime.Cache.Insert("Time", DateTime.Now.ToString(), null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(10.0));

使用Cache.NoAbsoluteExpiration跳过设置绝对时间

数据库依赖

  1. 数据库缓存依赖

-S服务器名称 -E集成身份验证 -ed启动 -d数据库名称 -et指定缓冲依赖的表名 -t表名
在vs2010的命令提示符中运行(切换到aspnet_regsql.exe所在的目录)
aspnet_regsql -S steve-pc -E -ed -d apsxDb -et -t TblComments

缓存依赖禁用该数据库
aspnet_regsql -S steve-pc -E -dd -d apsxDb

  1. 依赖于数据库的web.config配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <system.web>
    <caching>
    <sqlCacheDependency enabled="true">
    <databases>
    <add name="apsxDbEntityName" connectionStringName="connStr" pollTime="500"/>
    </databases>
    </sqlCacheDependency>
    </caching>
    </system.web>
  2. 1
    2
    System.Web.Caching.SqlCacheDependency dep = new System.Web.Caching.SqlCacheDependency("apsxDbEntityName", "Aspx_Students");//webconfig里面的配置名+表名
    Cache.Insert("list", list, dep);

缓存使用场景第一个:访问量大,变化比较少 典型场景:京东菜单项变化比较少,访问又特别大

HttpModule

创建一个实现了IHttpModule接口的类。
在Init方法中为HttpAplication参数注册事件。
在web.config中配置使用具体的HttpModule

1
2
3
<httpModules>
<add name="m1" type="Ajax.CRUD.UI.test2.TestModule,应用程序集的名称"/>
</httpModules>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class StopIP : IHttpModule
{
public void Dispose()
{
//throw new NotImplementedException();
}

public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}

void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication ha = sender as HttpApplication;
if (ha.Request.UserHostAddress == "192.168.1.100")
{
ha.Response.Redirect("~/EP1.htm");
}
}
}