Building a basic REST AspNet Core Example - Part 5
If you need to catch up on the previous posts then see part 1 & 2, 3, 4. The source code for this post is at github.
In this post I will look at additional HTTP features. We will revisit the case of when a resource was modified. We looked at ETag previously. In this post we will tackle the case where we want to go with a date for comparison instead. This should be done if generation of an ETag does not make sense for you.
When we looked at ETag in the InvoiceController we did not provide a Last-Modified response header. In the case of an invoice we would probably in real life also keep track of modification and thus we should provide this header according to RFC 7232
1
2
3
4
5
6
200 (OK) responses to GET or HEAD, an origin server:
o SHOULD send an entity-tag validator unless it is not feasible to
generate one.
...
o SHOULD send a Last-Modified value if it is feasible to send one.
Here we will look at a limited FilesController where files can be uploaded, and retrieved. I will just make enough code to highlight the HTTP features. We will add a simplified File class to the domain
A recipient MUST ignore If-Modified-Since if the request contains an If-None-Match header field; A recipient MUST ignore the If-Modified-Since header field if the received field-value is not a valid HTTP-date, or if the request method is neither GET nor HEAD.
You can find the description for the If-Modified-Since header in RFC 7232 section 3.4. The use case is as the standard says:
If-Unmodified-Since is most often used with state-changing methods (e.g., POST, PUT, DELETE) to prevent accidental overwrites when multiple user agents might be acting in parallel on a resource that does not supply entity-tags with its representations
Our example will be the DELETE method.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[HttpDelete("{id}")]
public IActionResult Delete(string id)
{
if (!fileRepository.Exists(id))
{
return NotFound();
}
// Yeah this you would never do in real life
var file = fileRepository.Get(id);
var requestHeaders = Request.GetTypedHeaders();
if (requestHeaders.IfMatch != null)
{
if (!requestHeaders.IfMatch.All(match => match.Equals(EntityTagHeaderValue.Any)))