asp.net-mvc - 为 FileResult 设置 ETag - MVC 3

标签 asp.net-mvc asp.net-mvc-3 etag

MVC 3 RTM。我有一个返回文件(图像/jpeg)的操作。我正在尝试为文件设置 ETag,但没有成功(etag 没有通过标题)。我已经尝试了 Response.Cache.SetETag 和 Response.AppenderHeader。如果我添加自己的自定义 header 标签,它会按预期工作,但似乎是我无法设置的 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();
}

行动结果:
    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-mvc - 为 FileResult 设置 ETag - MVC 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5031278/

相关文章:

javascript - AngularJS - 具有多个实例的 Controller 的表单验证

asp.net-mvc-3 - MVC3/Razor : cshtml. Execute()':找不到合适的方法来覆盖

extjs - 在Ext中删除_dc参数

java - 支持 ETags、If-Modified-Since 的 Android 图像加载器库

c# - 如何使用log4net创建log.txt?

asp.net-mvc - ASP.NET MVC 中 TempData 的范围和可见性是什么?

asp.net-mvc - Html.HiddenFor 值属性未设置

asp.net-mvc - httpErrors errorMode = "Detailed"仅适用于特定错误代码

c# - 实现扩展方法

apache - 如何生成和配置 ETag?