{"record":{"id":"ba397432dbac956b","repo":"dotnet/wpf","slug":"throw-new-argumentexception-sr","errorCode":null,"errorMessage":"throw new ArgumentException(SR.ContentTypeCannotHaveLeadingTrailingLWS);","messagePattern":"throw new ArgumentException\\(SR\\.ContentTypeCannotHaveLeadingTrailingLWS\\);","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/ContentType.cs","lineNumber":82,"sourceCode":"        /// we used more as an indication of an absent/unknown ContentType. \n        /// </summary>\n        /// <param name=\"contentType\">content-type</param>\n        /// <exception cref=\"ArgumentNullException\">If the contentType parameter is null</exception>\n        /// <exception cref=\"ArgumentException\">If the contentType string has leading or \n        /// trailing Linear White Spaces(LWS) characters</exception>\n        /// <exception cref=\"ArgumentException\">If the contentType string invalid CR-LF characters</exception>\n        internal ContentType(string contentType)\n        {\n            ArgumentNullException.ThrowIfNull(contentType);\n\n            if (contentType.Length == 0)\n            {\n                _contentType = String.Empty;\n            }\n            else\n            {\n                if (IsLinearWhiteSpaceChar(contentType[0]) || IsLinearWhiteSpaceChar(contentType[contentType.Length - 1]))\n                    throw new ArgumentException(SR.ContentTypeCannotHaveLeadingTrailingLWS);\n\n                //Carriage return can be expressed as '\\r\\n' or '\\n\\r'\n                //We need to make sure that a \\r is accompanied by \\n\n                ValidateCarriageReturns(contentType);\n\n                //Begin Parsing                \n                int semiColonIndex = contentType.IndexOf(_semicolonSeparator);\n\n                if (semiColonIndex == -1)\n                {\n                    // Parse content type similar to - type/subtype\n                    ParseTypeAndSubType(contentType);\n                }\n                else\n                {\n                    // Parse content type similar to - type/subtype ; param1=value1 ; param2=value2 ; param3=\"value3\"\n                    ParseTypeAndSubType(contentType.AsSpan(0, semiColonIndex));\n                    ParseParameterAndValue(contentType.AsSpan(semiColonIndex));","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/ContentType.cs#L64-L100","documentation":"The ContentType constructor parses an RFC-style media type string (e.g. for System.IO.Packaging). It rejects content types that begin or end with linear white space (spaces, tabs, CR/LF), throwing ArgumentException because leading/trailing LWS makes the value structurally invalid.","triggerScenarios":"new ContentType(\" text/plain\"), new ContentType(\"text/plain \"), or strings read from config/files with stray whitespace; also any string whose first or last char is \\r, \\n, \\t, or space.","commonSituations":"Hand-written content types in PackagePart.CreatePart calls, values parsed from .rels or [Content_Types].xml with formatting, trimming forgotten on user/config input.","solutions":["Trim the string before constructing: contentType.Trim().","Validate the input starts with the type token and ends with a parameter value or subtype.","Check the source (config, XML attribute) for accidental whitespace or line wrapping."],"exampleFix":"// before\nvar ct = new ContentType(userValue);\n// after\nvar ct = new ContentType(userValue.Trim());","handlingStrategy":"validation","validationCode":"public static bool IsValidContentTypeRaw(string s) =>\n    !string.IsNullOrEmpty(s) &&\n    !char.IsWhiteSpace(s[0]) && !char.IsWhiteSpace(s[s.Length - 1]);","typeGuard":null,"tryCatchPattern":"try { var ct = new ContentType(value); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"white space\"))\n{ value = value.Trim(); /* retry */ }","preventionTips":["Always .Trim() content-type strings sourced from config, XML, or user input.","Reject values containing any linear white space at the boundaries at the input boundary."],"tags":["contenttype","whitespace","argument","packaging","validation"],"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"}