EntityTagHeaderValue Class

Represents an Entity-Tag header value.

Namespace: System.Net.Http.Headers

Assembly: System.Net.Http.dll

Remarks

An entity-tag is a header name in HTTP used to provide an identifier for a version of a resource. The ETag header is returned by an HTTP server in response to a conditional HTTP request. The client can then use this ETag in subsequent requests to specify the version of the resource it has. If the resource has not changed, the server can return a 304 Not Modified status code. If the resource has changed, the server returns the updated resource. The EntityTagHeaderValue class provides a way to parse and manipulate Entity-Tag header values.

Constructors

EntityTagHeaderValue Constructor

Initializes a new instance of the EntityTagHeaderValue class.

EntityTagHeaderValue(string tag)

Initializes a new instance of the EntityTagHeaderValue class with the specified tag.

public EntityTagHeaderValue(string tag);

EntityTagHeaderValue(string tag, bool isWeak)

Initializes a new instance of the EntityTagHeaderValue class with the specified tag and weak indicator.

public EntityTagHeaderValue(string tag, bool isWeak);

Properties

IsTag
Gets a value indicating whether the Entity-Tag header value is a tag.
Tag
Gets the Entity-Tag header value.
IsWeak
Gets a value indicating whether the Entity-Tag header value is weak.

Methods

Parse Methods

These static methods are used to parse an Entity-Tag header string into an EntityTagHeaderValue object.

Parse(string value)

Parses a string into an EntityTagHeaderValue instance.

public static EntityTagHeaderValue Parse(string value);

TryParse(string value, out EntityTagHeaderValue result)

Tries to parse a string into an EntityTagHeaderValue instance.

public static bool TryParse(string value, out EntityTagHeaderValue result);

ToString Method

ToString()

Returns a string representation of the EntityTagHeaderValue.

public override string ToString();

Example

The following code example demonstrates how to create and use an EntityTagHeaderValue object.

using System; using System.Net.Http.Headers; public class Example { public static void Main() { // Create an EntityTagHeaderValue EntityTagHeaderValue etag = new EntityTagHeaderValue("\"abcdef-12345\""); Console.WriteLine($"ETag: {etag}"); Console.WriteLine($"Is Weak: {etag.IsWeak}"); Console.WriteLine($"Tag Value: {etag.Tag}"); // Parse an ETag header string headerString = "W/\"some-other-tag\""; if (EntityTagHeaderValue.TryParse(headerString, out EntityTagHeaderValue parsedEtag)) { Console.WriteLine($"Parsed ETag: {parsedEtag}"); Console.WriteLine($"Parsed Is Weak: {parsedEtag.IsWeak}"); Console.WriteLine($"Parsed Tag Value: {parsedEtag.Tag}"); } else { Console.WriteLine($"Failed to parse ETag: {headerString}"); } } }

See Also