MediaHeaderValue Class

Represents a media header value.

Class Information

This class represents a media header value, such as "application/json" or "text/plain; charset=utf-8". It provides properties and methods for parsing, manipulating, and formatting media header values.

Inheritance

Remarks

Media header values are used in HTTP headers like Content-Type and Accept to specify the media type of a resource or the media types that a client can accept.

This class supports the parsing and formatting of media type strings according to RFC 2045 and related specifications.

Examples

The following example demonstrates how to create a MediaHeaderValue object and add parameters to it.


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

public class Example
{
    public static void Main()
    {
        // Create a MediaHeaderValue for application/json
        MediaHeaderValue mediaValue = new MediaHeaderValue("application/json");

        // Add a parameter for charset
        mediaValue.CharSet = "utf-8";

        Console.WriteLine($"Media Header Value: {mediaValue}");
        // Output: Media Header Value: application/json; charset=utf-8

        // Create a MediaHeaderValue with parameters directly
        MediaHeaderValue anotherMediaValue = new MediaHeaderValue("text/plain");
        anotherMediaValue.Parameters.Add(new NameValueHeaderValue("version", "1.0"));

        Console.WriteLine($"Another Media Header Value: {anotherMediaValue}");
        // Output: Another Media Header Value: text/plain; version=1.0
    }
}

Class Declaration


public sealed class MediaHeaderValue : IEquatable<MediaHeaderValue>

Assembly

Assembly: System.Net.Http.dll

Namespace: System.Net.Http.Headers

Constructors

Properties

Methods