dotnet/wpf · error · ArgumentException

SR.InvalidTypeSubType

Error message

SR.InvalidTypeSubType

What it means

ContentType.ParseTypeAndSubType throws this ArgumentException when the type/subType portion of a MIME content-type string does not contain the mandatory '/' separator between type and subtype. The content type string being parsed is structurally invalid per the MIME grammar.

Solutions

  1. Ensure the value matches 'type/subType' with exactly one slash.
  2. Validate with a regex like ^[^/\s]+/[^/\s]+(;.*)?$ before constructing.
  3. Fix the source string (config, constant, file).

Example fix

// before
var ct = new ContentType("textplain");
// after
var ct = new ContentType("text/plain");
Defensive patterns

Strategy: validation

Validate before calling

public static bool IsTypeSubType(string s)
{
    if (string.IsNullOrEmpty(s)) return false;
    int first = s.IndexOf('/');
    return first > 0 && first < s.Length - 1 && s.IndexOf('/', first + 1) < 0;
}

Try / catch

try { var ct = new ContentType(value); }
catch (ArgumentException ex) when (ex.Message.Contains("type/subType"))
{ /* log and use a default content type */ }

Prevention

When it happens

Trigger: new ContentType("textplain") (missing slash), new ContentType("text/plain/extra"), or empty strings passing earlier checks; also '/' as first or last char.

Common situations: Typos in hand-written MIME strings, concatenating base type with an already-typed subtype, misparsed config values.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/5796578d9760fb37. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/ContentType.cs:414

            }
        }

        /// <summary>
        /// Parses the type ans subType tokens from the string. 
        /// Also verifies if the Tokens are valid as per the grammar.
        /// </summary>
        /// <param name="typeAndSubType">substring that has the type and subType of the content type</param>
        /// <exception cref="ArgumentException">If the typeAndSubType parameter does not have the "/" character</exception>
        private void ParseTypeAndSubType(ReadOnlySpan<char> typeAndSubType)
        {
            //okay to trim at this point the end of the string as Linear White Spaces(LWS) chars are allowed here.
            typeAndSubType = typeAndSubType.TrimEnd(_linearWhiteSpaceChars);

            int forwardSlashPos = typeAndSubType.IndexOf('/');
            if (forwardSlashPos < 0 || // no slashes
                typeAndSubType.Slice(forwardSlashPos + 1).IndexOf('/') >= 0) // more than one slash
            {
                throw new ArgumentException(SR.InvalidTypeSubType);
            }

            _type    = ValidateToken(typeAndSubType.Slice(0, forwardSlashPos).ToString());
            _subType = ValidateToken(typeAndSubType.Slice(forwardSlashPos + 1).ToString());
        }

        /// <summary>
        /// Parse the individual parameter=value strings
        /// </summary>
        /// <param name="parameterAndValue">This string has the parameter and value pair of the form
        /// parameter=value</param>
        /// <exception cref="ArgumentException">If the string does not have the required "="</exception>
        private void ParseParameterAndValue(ReadOnlySpan<char> parameterAndValue)
        {
            while (!parameterAndValue.IsEmpty)
            {
                //At this point the first character MUST be a semi-colon
                //First time through this test is serving more as an assert.

View on GitHub (pinned to 81131a70a4)