EntityTagHeaderParser Class
System.Net.Http.Headers
Parses the ETag header.
Syntax
public static class EntityTagHeaderParser
Methods
Parse(string value)
Parses an ETag header value.
public static EntityTagHeaderValue Parse(string value)
Parameters
string value: TheETagheader value to parse.
Return Value
An instance of EntityTagHeaderValue.
Exceptions
ArgumentNullException:valueis null.FormatException:valueis not a valid ETag header value.
TryParse(string value, out EntityTagHeaderValue result)
Tries to parse an ETag header value.
public static bool TryParse(string value, out EntityTagHeaderValue result)
Parameters
string value: TheETagheader value to parse.out EntityTagHeaderValue result: When this method returns, contains the parsedEntityTagHeaderValue.
Return Value
true if value is a valid ETag header value; otherwise, false.
Remarks
The EntityTagHeaderParser class is used internally by the HttpClient and related classes to parse the ETag header, which is used for cache validation.
An ETag header is a unique identifier for a specific version of a resource. It is used in conjunction with the If-None-Match request header to determine if a cached version of a resource is still current.
Example
The following code shows how to parse an ETag header value:
using System;
using System.Net.Http.Headers;
public class Example
{
public static void Main()
{
string etagValue = "\"abcde\"";
EntityTagHeaderValue etag;
if (EntityTagHeaderParser.TryParse(etagValue, out etag))
{
Console.WriteLine($"Successfully parsed ETag: {etag.Tag}");
Console.WriteLine($"IsStrong: {etag.IsStrong}");
}
else
{
Console.WriteLine("Failed to parse ETag.");
}
string invalidEtagValue = "invalid-etag";
if (!EntityTagHeaderParser.TryParse(invalidEtagValue, out etag))
{
Console.WriteLine($"Correctly identified invalid ETag: {invalidEtagValue}");
}
}
}