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.
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.
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
}
}
public sealed class MediaHeaderValue : IEquatable<MediaHeaderValue>
Assembly: System.Net.Http.dll
Namespace: System.Net.Http.Headers
MediaHeaderValue class.
Parameters:
mediaType: The media type.MediaHeaderValue class with a copy of another MediaHeaderValue instance.
Parameters:
mediaHeaderValue: The MediaHeaderValue instance to copy.Parameters:
input: The string to parse.Returns:
AMediaHeaderValue instance.
Exceptions:
Parameters:
input: The string to parse.mediaHeaderValue: When this method returns, contains the parsed MediaHeaderValue instance, if the parse succeeded, or null if the parse failed.Returns:
true if the string was parsed successfully; otherwise, false.