Last-Modified Header Value
Represents the value of the Last-Modified header.
Namespace
System.Net.Http.Headers
Assembly
System.Net.Http.Headers.dll
Remarks
The Last-Modified header is an entity-header that indicates the date and time at which the origin server last modified the representation of the target resource. The date and time value MUST be an IMF-fixdate format.
This class provides a strongly-typed way to represent and manipulate the Last-Modified header value.
Properties
- Date
- Gets the date and time associated with the
Last-Modifiedheader.
Constructors
The LastModifiedHeaderValue class has the following constructors:
public LastModifiedHeaderValue(DateTimeOffset date)
Initializes a new instance of the LastModifiedHeaderValue class with the specified date and time.
public LastModifiedHeaderValue(DateTimeOffset date, DateTimeStyles styles)
Initializes a new instance of the LastModifiedHeaderValue class with the specified date and time and parsing styles.
Methods
While specific methods are not directly exposed for manipulating the date itself (as it's handled by DateTimeOffset), the class provides methods for parsing and formatting the header value.
Parsing and Formatting
You can parse a string representation of the Last-Modified header into a LastModifiedHeaderValue object, and format a LastModifiedHeaderValue object back into a string.
// Example: Parsing a string
string headerString = "Tue, 15 Nov 1994 12:45:26 GMT";
LastModifiedHeaderValue lastModifiedValue = LastModifiedHeaderValue.Parse(headerString);
// Accessing the date
DateTimeOffset lastModifiedDate = lastModifiedValue.Date;
// Example: Formatting to a string
DateTimeOffset currentTime = DateTimeOffset.UtcNow;
LastModifiedHeaderValue newValue = new LastModifiedHeaderValue(currentTime);
string formattedHeader = newValue.ToString();
Static Methods
| Method | Description |
|---|---|
public static LastModifiedHeaderValue Parse(string input) |
Parses a string into an instance of LastModifiedHeaderValue. |
public static bool TryParse(string input, out LastModifiedHeaderValue parsedValue) |
Tries to parse a string into an instance of LastModifiedHeaderValue. Returns true if parsing succeeds, otherwise false. |
Usage Example
Here's a common scenario for using LastModifiedHeaderValue when working with HttpResponseMessage:
using System.Net.Http;
using System.Net.Http.Headers;
using System;
// ...
HttpResponseMessage response = new HttpResponseMessage();
DateTimeOffset lastModified = DateTimeOffset.UtcNow.AddHours(-24); // Resource last modified 24 hours ago
response.Headers.LastModified = new LastModifiedHeaderValue(lastModified);
// To retrieve it later:
if (response.Headers.LastModified != null)
{
DateTimeOffset date = response.Headers.LastModified.Date;
Console.WriteLine($"Resource last modified: {date}");
}