Asp.Net 自定义设置Http缓存示例(一)

时间:2024-12-18 11:05:08

一、自定义图片输出,启用客户端的图片缓存处理

代码示例:

                string path = Request.Url.LocalPath;
if (path != null)
{
path = path.ToLower();
if (path.Contains("uploadfiles") || path.Contains("ueditor/net/upload"))
{
path = Server.MapPath(path).Replace("LiuXue.Mobile", "LiuXue.Web");
//输出主站内容
if (File.Exists(path))
{
//自定义缓存处理
Response.ContentType = "image/jpeg";
Response.Headers["Cache-Control"] = "public"; //指定客户端,服务器都处理缓存
int length = path.LastIndexOf(".") - path.LastIndexOf("/") - ;
Response.Headers["Etag"] = path.Substring(path.LastIndexOf("/") + , length);
Response.Headers["Last-Modified"] = new DateTime().ToString("r");
Response.Headers["Content-Length"] = new FileInfo(path).Length.ToString();
Response.Headers["Accept-Ranges"] = "bytes"; Response.TransmitFile(path);
Response.End();
}
}
}

更多:

Asp.Net Core 自定义设置Http缓存处理

asp.net mvc输出自定义404等错误页面,非302跳转

Asp.Net Core获取请求上下文HttpContext