Skip to content

Instantly share code, notes, and snippets.

@benfoster
Created July 20, 2012 13:01
Show Gist options
  • Save benfoster/3150615 to your computer and use it in GitHub Desktop.
Save benfoster/3150615 to your computer and use it in GitHub Desktop.
Setting response properties
static Dictionary<int, string> etags = new Dictionary<int, string>();
// GET api/site/5
public HttpResponseMessage Get(int id, HttpRequestMessage request)
{
var etag = request.Headers.IfNoneMatch; // strange that we can't do FirstOrDefault()
if (etag != null && etags.ContainsKey(id))
{
if (etags[id] == etag.ToString())
{
var notModified = new HttpResponseMessage(HttpStatusCode.NotModified);
return notModified;
}
}
var site = session.Load<Site>(id);
if (site == null || site.Deleted)
ThrowSiteNotFound();
var metadata = session.Advanced.GetMetadataFor(site);
var siteEtag = metadata.Value<string>("@etag");
etags[id] = siteEtag;
var response = Request.CreateResponse<Site>(HttpStatusCode.OK, site);
response.Headers.ETag = new EntityTagHeaderValue("\"" + siteEtag + "\"");
response.Content.Headers.LastModified = metadata.Value<DateTime>("Last-Modified");
return response;
}
@benfoster
Copy link
Author

Okay. Good to know. I've just added CacheCow as suggested and this looks like it will be much easier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment