{"record":{"id":"8bc5ca2784e6ae93","repo":"OrchardCMS/OrchardCore","slug":"the-displayname-can-t-be-null-or-empty","errorCode":null,"errorMessage":"The 'displayName' can't be null or empty.","messagePattern":"The 'displayName' can't be null or empty\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore.Modules/OrchardCore.ContentTypes/Services/ContentDefinitionService.cs","lineNumber":53,"sourceCode":"        foreach (var element in contentParts.Select(x => x.GetType()))\n        {\n            logger.LogError(\"The content part '{ContentPart}' should not be registered in DI. Use AddContentPart<T>() instead.\", element);\n        }\n\n        foreach (var element in contentFields.Select(x => x.GetType()))\n        {\n            logger.LogError(\"The content field '{ContentField}' should not be registered in DI. Use AddContentField<T>() instead.\", element);\n        }\n\n        _logger = logger;\n        S = stringLocalizer;\n    }\n\n    public async Task<ContentTypeDefinition> AddTypeAsync(string name, string displayName)\n    {\n        if (string.IsNullOrWhiteSpace(displayName))\n        {\n            throw new ArgumentException($\"The '{nameof(displayName)}' can't be null or empty.\", nameof(displayName));\n        }\n\n        if (string.IsNullOrWhiteSpace(name))\n        {\n            name = await GenerateContentTypeNameFromDisplayNameAsync(displayName);\n        }\n        else\n        {\n            if (!char.IsAsciiLetter(name[0]))\n            {\n                throw new ArgumentException(\"Content type name must start with a letter\", nameof(name));\n            }\n            if (!string.Equals(name, name.ToSafeName(), StringComparison.OrdinalIgnoreCase))\n            {\n                throw new ArgumentException(\"Content type name contains invalid characters\", nameof(name));\n            }\n        }\n","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore.Modules/OrchardCore.ContentTypes/Services/ContentDefinitionService.cs#L35-L71","documentation":"ContentDefinitionService.AddTypeAsync creates a new content type and requires a non-empty display name, since the display name drives the admin UI label and (when no name is supplied) the generated technical name. It throws ArgumentException with the parameter name when displayName is null, empty, or whitespace.","triggerScenarios":"Calling AddTypeAsync(name, displayName) with null/empty/whitespace displayName — e.g. an admin 'Create new type' form submitted blank, or custom code passing an unset variable.","commonSituations":"Custom type-creation tooling or scripts that skip UI validation; automated provisioning code building types from user input without checking the display name.","solutions":["Supply a meaningful displayName before calling AddTypeAsync.","Validate with string.IsNullOrWhiteSpace on the caller side and show a form validation error instead.","If only a technical name is known, derive a display name from it before invoking the service."],"exampleFix":"// before\nawait _contentDefinitionService.AddTypeAsync(\"Product\", \"\");\n// after\nif (string.IsNullOrWhiteSpace(displayName)) throw new ValidationException(\"Display name is required\");\nawait _contentDefinitionService.AddTypeAsync(\"Product\", displayName);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(displayName))\n    throw new ArgumentException(\"Display name is required.\", nameof(displayName));","typeGuard":"static bool IsValidDisplayName(string? s) => !string.IsNullOrWhiteSpace(s);","tryCatchPattern":"try { await _service.AddTypeAsync(name, displayName); }\ncatch (ArgumentException ex) when (ex.ParamName == nameof(displayName))\n{\n    ModelState.AddModelError(nameof(displayName), \"Display name is required.\");\n    return View(model);\n}","preventionTips":["Mark the display-name field [Required] in admin forms","Always validate user input before service calls","Never pass unset variables as displayName","Derive display names from identifiers when only a name is known"],"tags":["contenttypes","argument-validation","display-name"],"backgroundTag":"empty-required-field","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}