how to Add an Expires or a Cache-Control Header

sohighthesky 2010-01-28 04:20:14
YSLow Result:
1.Grade F on Add Expires headers
There are 29 static components without a far-future expiration date.
2:Grade F on Configure entity tags (ETags)
There are 29 components with misconfigured ETags

how can i add Expires headers for all images,flash ect. in asp.net application

在asp.net要怎样配置过期时间和ETags呢?
我在httpModule中配置了结果报错说已设置ETags,请问大家是怎么配置的呢?
code:

using System;

namespace WebModule
{
using System.Web;
using System.IO.Compression;
using System.Text.RegularExpressions;
/// <summary>
/// Compresses the output using standard gzip/deflate.
/// eg:
/// <httpModules>
/// <add type="WebModule.CompressModule,WebHandler" name="CompressModule1" />
/// </httpModules>
/// </summary>
public class CacheModule : IHttpModule
{
void IHttpModule.Dispose()
{
// Nothing to dispose;
}
void IHttpModule.Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}

/// <summary>
/// 获取配置文件中要要启用缓存的拓展名
/// </summary>
private static string EXT = string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["CacheEXT"]) ? "jpg|gif" : System.Configuration.ConfigurationManager.AppSettings["CacheEXT"];
private static TimeSpan MAXAGE = new TimeSpan(7, 0, 0, 0);
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
if (!Regex.IsMatch(app.Request.FilePath, ".(?:" + EXT + ")$", RegexOptions.IgnoreCase))
return;

SetHeaders(System.IO.File.GetLastWriteTime(app.Server.MapPath(app.Request.FilePath)).GetHashCode(), app.Context, MAXAGE);
}

#region SetHeaders
/// <summary>
/// This will make the browser and server keep the output
/// in its cache and thereby improve performance.
/// </summary>
public static void SetHeaders(int hash, HttpContext context,TimeSpan maxAge)
{
//context.Response.ContentType = contentType;
context.Response.Cache.VaryByHeaders["Accept-Encoding"] = true;

context.Response.Cache.SetExpires(DateTime.Now.ToUniversalTime().Add(maxAge));
context.Response.Cache.SetMaxAge(maxAge);
context.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);

string etag = "\"" + hash.ToString() + "\"";
context.Response.Cache.SetETag(etag);

if (String.Compare(context.Request.Headers["If-None-Match"], etag) == 0)
{
context.Response.Clear();
context.Response.StatusCode = (int)System.Net.HttpStatusCode.NotModified;
context.Response.SuppressContent = true;
}
}
#endregion

}
}


相关说明:

Add an Expires or a Cache-Control Header
Configure ETags
...全文
957 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
sohighthesky 2010-02-01
  • 打赏
  • 举报
回复
up
sohighthesky 2010-01-30
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 seesea125 的回复:]
没明白你什么意思,你是要页面打开默认不开启cache吗,如果是这样可以这么限定一下就可以
protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        Response.ContentEncoding = Encoding.UTF8;
        Response.Cache.SetExpires(DateTime.Now.AddMinutes(-60));
        Response.Cache.SetCacheability(HttpCacheability.Private);
        Response.Cache.SetValidUntilExpires(False);
}
[/Quote]
我是要为全站的图片等文件开启缓存 ,所以在httpModule里
artwl_cn 2010-01-29
  • 打赏
  • 举报
回复
sohighthesky
(//珍爱生命,远离CSDN !)

何解???
sohighthesky 2010-01-29
  • 打赏
  • 举报
回复
不会的帮顶一下,没人解决就当散分了
有人解决就加100分
seesea125 2010-01-29
  • 打赏
  • 举报
回复
没明白你什么意思,你是要页面打开默认不开启cache吗,如果是这样可以这么限定一下就可以
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Response.ContentEncoding = Encoding.UTF8;
Response.Cache.SetExpires(DateTime.Now.AddMinutes(-60));
Response.Cache.SetCacheability(HttpCacheability.Private);
Response.Cache.SetValidUntilExpires(False);
}
sunhahaha2 2010-01-29
  • 打赏
  • 举报
回复
帮顶 记号
wosizy 2010-01-29
  • 打赏
  • 举报
回复
关注下!···
gujunfenzhan 2010-01-29
  • 打赏
  • 举报
回复
UP
sohighthesky 2010-01-29
  • 打赏
  • 举报
回复
newdigitime 2010-01-29
  • 打赏
  • 举报
回复
context.Response.Cache.SetExpires可以啊.只不过你要开启 用户端的cache

或者
Response.ExpiresAbsolute=DateTime.Now.AddSeconds(xxxx);
ssy888 2010-01-29
  • 打赏
  • 举报
回复
回家慢慢看,先顶了。
sohighthesky 2010-01-29
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 plglenn9 的回复:]
叫菲菲童鞋吧
[/Quote]
你叫
happy664618843 2010-01-29
  • 打赏
  • 举报
回复
再顶
happy664618843 2010-01-29
  • 打赏
  • 举报
回复
帮顶了
huwei12345 2010-01-29
  • 打赏
  • 举报
回复
那就顶吧?
plglenn9 2010-01-29
  • 打赏
  • 举报
回复
叫菲菲童鞋吧
Jelindu 2010-01-28
  • 打赏
  • 举报
回复
httpModule与HttpHandler,都是最底层的运行模块,你界面配置了只是给我们一个可以自己处理程序接口而已,
配置过期时间跟httpModule与HttpHandler有什么关系?
丰云 2010-01-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 mengfanzongfox 的回复:]
头像真恶心鄙视
[/Quote]
haha
mengfanzongfox 2010-01-28
  • 打赏
  • 举报
回复
头像真恶心鄙视
lovexilove 2010-01-28
  • 打赏
  • 举报
回复
up
加载更多回复(1)

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧