Why does HttpCacheability.Private suppress ETags?

前端 未结 3 1651
天命终不由人
天命终不由人 2020-12-30 22:36

While writing a custom IHttpHandler I came across a behavior that I didn\'t expect concerning the HttpCachePolicy object.

My handler calculates and sets an entity-ta

相关标签:
3条回答
  • 2020-12-30 23:12

    Unfortunately if you look at System.Web.HttpCachePolicy.UpdateCachedHeaders() in .NET Reflector you see that there's an if statement specifically checking that the Cacheability is not Private before doing any ETag stuff. In any case, I've always found that Last-Modified/If-Modified-Since works well for our data and is a bit easier to monitor in Fiddler anyway.

    0 讨论(0)
  • 2020-12-30 23:27

    If like me you're unhappy with the workaround mentioned here of using Cacheability.ServerAndPrivate, and you really want to use Private instead - perhaps because you are customising pages individually for users and it makes no sense to cache on the server - then at least in .NET 3.5 you can set ETag through Response.Headers.Add and this works fine.

    N.B. if you do this you have to implement the comparison of the client headers yourself and the HTTP 304 response handling - not sure if .NET takes care of this for you under normal circumstances.

    0 讨论(0)
  • 2020-12-30 23:32

    I think you need to use HttpCacheability.ServerAndPrivate

    That should give you cache-control: private in the headers and let you set an ETag.

    The documentation on that needs to be a bit better.

    Edit: Markus found that you also have call cache.SetOmitVaryStar(true) otherwise the cache will add the Vary: * header to the output and you don't want that.

    0 讨论(0)
提交回复
热议问题