Created
July 20, 2012 13:01
-
-
Save benfoster/3150615 to your computer and use it in GitHub Desktop.
Setting response properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Okay. Good to know. I've just added CacheCow as suggested and this looks like it will be much easier.