asp.net-mvc – 为FileResult设置ETag – MVC 3
发布时间:2020-05-28 13:33:01 所属栏目:asp.Net 来源:互联网
导读:MVC 3 RTM.我有一个返回文件的动作(image / jpeg).我试图为一个文件设置ETag没有成功(etag没有通过标题).我已经尝试了Response.Cache.SetETag和Response.AppenderHeader.如果我添加自己的自定义标头标签,它按预期工作,它似乎是我无法设置的ETag. 这是来源. [H
|
MVC 3 RTM.我有一个返回文件的动作(image / jpeg).我试图为一个文件设置ETag没有成功(etag没有通过标题).我已经尝试了Response.Cache.SetETag和Response.AppenderHeader.如果我添加自己的自定义标头标签,它按预期工作,它似乎是我无法设置的ETag. 这是来源. [HttpGet,OutputCache(Location= OutputCacheLocation.Client,VaryByParam="userId",Duration=3600,NoStore=true)]
public ActionResult GetImage(string userId)
{
byte[] result;
using (var client = new WebClient())
{
client.Credentials = CredentialCache.DefaultCredentials;
result = client.DownloadData(string.Format(IntranetUrl,userId));
}
Response.Cache.SetETag("00amyWGct0y_ze4lIsj2Mw");
//or Response.AppendHeader("ETag","00amyWGct0y_ze4lIsj2Mw");
Response.AppendHeader("MyHeader","HelloWorld");
return File(result,"image/jpeg");
}
这是资源请求/响应: > Request > URL:http://localhost/MyApp/Employee.mvc/GetImage?userId=myUserId > Request Method:GET Status Code:200 OK > Request Headers Accept:*/* > Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3 > Accept-Encoding:gzip,deflate,sdch > Accept-Language:en-US,en;q=0.8 > Cache-Control:max-age=0 > Connection:keep-alive > Cookie:ASP.NET_SessionId=mySessionId > Host:localhost > Referer:http://localhost/MyApp/Employee/Review/24/Index > User-Agent:Mozilla/5.0 (Windows; U; > Windows NT 5.1; en-US) > AppleWebKit/534.13 (KHTML,like Gecko) > Chrome/9.0.597.98 Safari/534.13 Query > String Parameters userId:myUserId > Response Headers > Cache-Control:private,no-store,> max-age=3600 Content-Length:1428 > Content-Type:image/jpeg Date:Thu,17 > Feb 2011 15:45:30 GMT Expires:Thu,17 > Feb 2011 16:45:29 GMT > Last-Modified:Thu,17 Feb 2011 > 15:45:29 GMT MyHeader:HelloWorld > Server:Microsoft-IIS/5.1 > X-AspNet-Version:4.0.30319 > X-AspNetMvc-Version:3.0 > X-Powered-By:ASP.NET 更新 我把所有的代码都删除了,但仍然没有… 行动: public FileETagActionResult GetImage()
{
return new FileETagActionResult();
}
的ActionResult: public class FileETagActionResult : ActionResult
{
public override void ExecuteResult(ControllerContext context)
{
byte[] result;
using (var client = new WebClient())
{
result = client.DownloadData("http://myintranet/images/logo.png");
}
var hash = MD5.Create().ComputeHash(result);
string etag = String.Format(""{0}"",Convert.ToBase64String(hash));
TimeSpan expireTs = TimeSpan.FromDays(5);
context.HttpContext.Response.Cache.SetCacheability(HttpCacheability.Private);
context.HttpContext.Response.Cache.SetETag(etag);
context.HttpContext.Response.Cache.SetExpires(DateTime.Now.AddDays(5));
context.HttpContext.Response.Cache.SetMaxAge(expireTs);
context.HttpContext.Response.ContentType = "image/png";
context.HttpContext.Response.BinaryWrite(result);
}
}
解决方法如果您使用HttpCacheability.Private,将禁止ETag.您可以在此找到有关此question的更多信息 如果你把它改成HttpCacheability.ServerAndPrivate它应该工作 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 手动更新表单认证券:
- asp.net – 格式化DataBinder.Eval数据
- asp.net-mvc-3 – 发布表单时控件中的模型绑定 – 不会自动
- asp.net-mvc – 在我正在运行的Visual Studio 2013中的另一
- asp.net-mvc – 在ASP.NET MVC Preview 4中使用路由引擎进行
- asp.net – 如何在SQL4的log4net配置中使用存储过程进行日志
- 如何在服务器端缓存ASP.NET自定义HttpHandler响应
- asp.net – 转发器控件中的单选按钮列表
- asp.net – 使用OLEDB读取CSV文件,即使连接字符串中的HDR =
- asp.net-mvc – 无法导出Kendo Grid中的隐藏列
推荐文章
站长推荐
- entity-framework – 在Junction表上使用EF Core
- ASP.NET – 从内容页面访问主页面元素
- asp.net-mvc – 验证:Model或ViewModel
- asp.net-mvc – ASP.Net MVC风格包不包括大多数文
- asp.net-mvc – 使用Automapper将字符串映射到枚
- 停止代码构建器转义单引号ASP.NET MVC 2
- asp.net-mvc – MVCSiteMapProvider中的全球化
- asp.net – 当使用AngularJS,WebAPI 2和Oauth 2时
- asp.net – 通过Ajax Post – MVC3更新模型更改视
- asp.net-mvc – 当我使用Validator.TryValidateO
热点阅读
