本文介绍了设置为ETag的FileResult - MVC 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MVC 3 RTM。我有一个返回文件(影像/ JPEG)的动作。我想设置的ETag对于没有成功(ETag的不来通过头)的文件。我曾经尝试都Response.Cache.SetETag和Response.AppenderHeader。如果我添加自己的自定义标题标签,它按预期工作,它只是似乎是ETag的,我不能设置。

下面是源。

  [HTTPGET,的OutputCache(位置= OutputCacheLocation.Client,的VaryByParam =用户id,持续时间= 3600,NoStore = TRUE)]
        公众的ActionResult的getImage(字符串userid)
        {
            字节[]的结果;            使用(VAR的客户=新的WebClient())
            {
                client.Credentials = CredentialCache.DefaultCredentials;
                结果= client.DownloadData(的String.Format(IntranetUrl,用户id));
            }            Response.Cache.SetETag(00amyWGct0y_ze4lIsj2Mw);
            //或Response.AppendHeader(ETag的,00amyWGct0y_ze4lIsj2Mw);
            Response.AppendHeader(MyHeader,的HelloWorld);            返回文件(的结果,图像/ JPEG);
        }

和这里的资源请求/响应:

 >请求
>网址:HTTP:?//localhost/MyApp/Employee.mvc/GetImage用户id = myUserId
>请求方法:GET状态code:200 OK
>请求头接受:* / *
>接收字符集:ISO-8859-1,UTF-8,Q = 0.7 *; Q = 0.3
>接受编码:gzip,紧缩,SDCH
>接受语言:EN-US,EN; Q = 0.8
>缓存控制:最大年龄= 0
>连接:保持活动
>饼干:ASP.NET_SessionId = mySessionId
>主持人:本地主机
>引用者:HTTP://本地主机/ MyApp的/员工/评论/ 24 /索引
>用户代理:Mozilla的/ 5.0(视窗; U;
> Windows NT的5.1; EN-US)
>为AppleWebKit / 534.13(KHTML,例如Gecko)
>镀铬/ 9.0.597.98 Safari / 534.13查询
>字符串参数用户名:myUserId
>响应头
>缓存控制:私人的,没有商店,
>最大年龄= 3600的Content-Length:1428
>内容类型:图像/ JPEG日期:星期四,17
> 2011年2月15点45分30秒格林尼治标准​​时间截止日期:周四,17
> 2011 GMT 16时45分29秒二月
>上次修改:周四,2011年2月17日
> 15点45分29秒格林尼治标准​​时间MyHeader:的HelloWorld
>服务器:Microsoft-IIS / 5.1
>的X ASPNET-版本:4.0.30319
>的X AspNetMvc-版本:3.0
>的X已启动方式:ASP.NET

更新

我已经全部剥离code到这一点,仍然没有去...

动作:

 公共FileETagActionResult的getImage()
{
    返回新FileETagActionResult();
}

的ActionResult:

 公共类FileETagActionResult:的ActionResult
    {        公共覆盖无效的ExecuteReuslt(ControllerContext上下文)
        {            字节[]的结果;            使用(VAR的客户=新的WebClient())
            {
                结果= client.DownloadData(HTTP://myintranet/images/logo.png);
            }            VAR哈希= MD5.Create()ComputeHash(结果)。
            字符串ETAG =的String.Format(\\{0} \\,Convert.ToBase64String(散));            时间跨度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 =图像/ PNG
            context.HttpContext.Response.BinaryWrite(结果);
        }
    }


解决方案

的eTag将燮pressed如果你使用 HttpCacheability.Private

您可以找到更多这方面的信息,<一个href=\"http://stackoverflow.com/questions/32824/why-does-httpcacheability-private-sup$p$pss-etags\">question

如果您将其更改为 HttpCacheability.ServerAndPrivate 它应该工作

MVC 3 RTM. I have an action that returns a file (image/jpeg). I am trying to set the ETag for a file with no success (the etag doesn't come through on header). I have tried both Response.Cache.SetETag and Response.AppenderHeader. If I add my own custom header tag, it works as expected, it just seems to be ETag that I can't set.

Here is the source.

        [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");
        }

And here is the resource request/response:

> 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

Update

I've stripped all the code down to this and still no go...

Action:

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);


        }
    }
解决方案

The ETag will be suppressed if you use HttpCacheability.Private.

You can find more information on this question

If you change it to HttpCacheability.ServerAndPrivate it should work

这篇关于设置为ETag的FileResult - MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 20:21