System.Net.Http.Headers

LinkHeaderValue Class

Represents the value of the Link HTTP header.

The Link header field provides a means for a client or server to indicate the relative arbitrary targets for the client or server, and the relationship of those targets to the current request.

Namespace

System.Net.Http.Headers

Assembly

System.Net.Http.dll

Inheritance

Remarks

The LinkHeaderValue class is used to parse and represent the value of the Link HTTP header. This header is defined in RFC 5988.

A typical Link header value might look like this:

<http://example.com/page1>; rel="next"; title="Next Page"

This class provides properties to access the URI and the parameters associated with the link.

Properties

Name Description
Uri Gets the URI for the linked resource.
Parameters Gets the collection of parameters for the link.

Methods

Name Description
Parse(String, IFormatProvider) Parses a string into a LinkHeaderValue instance.
ToString() Converts the LinkHeaderValue object to its string representation.

Example Usage

The following example demonstrates how to create and use a LinkHeaderValue.


using System;
using System.Net.Http.Headers;

public class Example
{
    public static void Main(string[] args)
    {
        // Create a LinkHeaderValue
        var link = new LinkHeaderValue(new Uri("http://example.com/next"))
        {
            ["rel"] = "next",
            ["title"] = "Next Page"
        };

        // Output the string representation
        Console.WriteLine(link.ToString());
        // Expected output: <http://example.com/next>; rel="next"; title="Next Page"

        // Parse a string into a LinkHeaderValue
        string linkString = "<http://example.com/previous>; rel=\"prev\"; title=\"Previous Page\"";
        LinkHeaderValue parsedLink = LinkHeaderValue.Parse(linkString, null);

        Console.WriteLine($"Parsed URI: {parsedLink.Uri}");
        Console.WriteLine($"Parsed rel: {parsedLink.Parameters.First(p => p.Name == "rel").Value}");
        Console.WriteLine($"Parsed title: {parsedLink.Parameters.First(p => p.Name == "title").Value}");
    }
}

See Also