{"record":{"id":"cc836dd2117fe514","repo":"dotnet/wpf","slug":"throw-new-argumentexception-sr-contenttype","errorCode":null,"errorMessage":"throw new ArgumentException(SR.InvalidLinearWhiteSpaceCharacter);","messagePattern":"throw new ArgumentException\\(SR\\.InvalidLinearWhiteSpaceCharacter\\);","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/ContentType.cs","lineNumber":395,"sourceCode":"        /// <param name=\"contentType\"></param>\n        private static void ValidateCarriageReturns(string contentType)\n        {\n            Debug.Assert(!IsLinearWhiteSpaceChar(contentType[0]) && !IsLinearWhiteSpaceChar(contentType[contentType.Length - 1]));\n\n            //Prior to calling this method we have already checked that first and last\n            //character of the content type are not Linear White Spaces. So its safe to\n            //assume that the index will be greater than 0 and less that length-2.\n\n            int index = contentType.IndexOf(_linearWhiteSpaceChars[2]);\n            \n            while (index != -1)\n            {\n                if (contentType[index - 1] == _linearWhiteSpaceChars[1] || contentType[index + 1] == _linearWhiteSpaceChars[1])\n                {\n                    index = contentType.IndexOf(_linearWhiteSpaceChars[2], ++index);\n                }\n                else\n                    throw new ArgumentException(SR.InvalidLinearWhiteSpaceCharacter);\n            }\n        }\n\n        /// <summary>\n        /// Parses the type ans subType tokens from the string. \n        /// Also verifies if the Tokens are valid as per the grammar.\n        /// </summary>\n        /// <param name=\"typeAndSubType\">substring that has the type and subType of the content type</param>\n        /// <exception cref=\"ArgumentException\">If the typeAndSubType parameter does not have the \"/\" character</exception>\n        private void ParseTypeAndSubType(ReadOnlySpan<char> typeAndSubType)\n        {\n            //okay to trim at this point the end of the string as Linear White Spaces(LWS) chars are allowed here.\n            typeAndSubType = typeAndSubType.TrimEnd(_linearWhiteSpaceChars);\n\n            int forwardSlashPos = typeAndSubType.IndexOf('/');\n            if (forwardSlashPos < 0 || // no slashes\n                typeAndSubType.Slice(forwardSlashPos + 1).IndexOf('/') >= 0) // more than one slash\n            {","sourceCodeStart":377,"sourceCodeEnd":413,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/ContentType.cs#L377-L413","documentation":"During ContentType parsing, ValidateCarriageReturns ensures every '\\r' is part of a proper '\\r\\n' (or '\\n\\r') pair used as LWS folding. An isolated carriage return or stray line-feed inside the content type throws ArgumentException (InvalidLinearWhiteSpaceCharacter).","triggerScenarios":"new ContentType(...) with a string containing a bare '\\r' not adjacent to '\\n' (or bare '\\n' not adjacent to '\\r'), e.g. values assembled by string concatenation with Environment.NewLine on mixed conventions or raw multi-line input.","commonSituations":"Content types copied from headers with line folding, files with Windows/Unix newline mismatch, template-built strings where a newline slipped in.","solutions":["Strip all control characters/newlines from the content type string before construction.","Replace \"\\r\\n\" sequences with a single space if folding was intended.","Sanitize at input boundary: filter chars outside the printable token/parameter grammar."],"exampleFix":"// before\nvar ct = new ContentType(\"text/plain\" + Environment.NewLine);\n// after\nvar ct = new ContentType((\"text/plain\" + Environment.NewLine).Trim(new[] {'\\r','\\n',' ','\\t'}));","handlingStrategy":"validation","validationCode":"public static string StripControlChars(string s) =>\n    new string(s.Where(c => !char.IsControl(c)).ToArray());","typeGuard":null,"tryCatchPattern":"try { var ct = new ContentType(value); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"white space character\"))\n{ value = StripControlChars(value); }","preventionTips":["Remove CR/LF/control characters from strings before passing to ContentType.","Be careful concatenating with Environment.NewLine.","Sanitize content copied from HTTP headers or multi-line files."],"tags":["contenttype","newline","carriage-return","validation","packaging"],"backgroundTag":"invalid-argument-format","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}