OrchardCMS/OrchardCore · error · ArgumentException
The type must inherit from ContentPart
Error message
The type must inherit from ContentPart
What it means
ContentOptions.GetOrAddContentPart throws ArgumentException when the given Type does not derive from ContentPart. Only ContentPart subclasses can have content part options registered in the content metadata options.
Solutions
- Make sure the type passed to AddContentPart<T> inherits from ContentPart.
- If you meant to register a field, call AddContentField<T> instead.
- Check for accidental use of a ContentPartOption/other wrapper type as the generic argument.
Example fix
// before services.AddContentPart<MyField>(); // MyField : ContentField // after services.AddContentField<MyField>();
Defensive patterns
Strategy: validation
Validate before calling
if (!typeof(ContentPart).IsAssignableFrom(typeof(T)))
throw new ArgumentException("T must inherit from ContentPart"); Type guard
bool IsContentPart(Type t) => typeof(ContentPart).IsAssignableFrom(t) && !t.IsAbstract; // adjust as needed
Try / catch
try
{
services.AddContentPart<T>();
}
catch (ArgumentException e)
{
throw new InvalidOperationException("AddContentPart requires a ContentPart subclass", e);
} Prevention
- Check the class hierarchy before registering: parts extend ContentPart, fields extend ContentField.
- Use AddContentField<T> for fields, AddContentPart<T> for parts.
- Centralize part/field registration in one Startup location for review.
When it happens
Trigger: Calling AddContentPart<T> (or ContentOptions.GetOrAddContentPart directly) with a type that is not a ContentPart subclass, e.g. a ContentField, ContentPartOption wrapper, interface, or plain POCO.
Common situations: Copy-paste registering a field type with AddContentPart, wrong generic parameter in Startup.cs, registering a view model or record instead of the part class.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- The type must inherit from ContentField
- Failed casting content to
- Content part name must start with a letter
- Content part name contains invalid characters
- Content part name is reserved for internal use
AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13).
Data as JSON: /api/errors/9ae3982a19ceed79.
Report an issue: GitHub.
Appendix: source
Thrown at src/OrchardCore/OrchardCore.ContentManagement.Abstractions/ContentOptions.cs:40
public IReadOnlyList<ContentFieldOption> ContentFieldOptions => _contentFields;
internal void AddPartHandler(Type contentPartType, Type handlerType)
{
var option = GetOrAddContentPart(contentPartType);
option.AddHandler(handlerType);
}
internal void RemovePartHandler(Type contentPartType, Type handlerType)
{
var option = GetOrAddContentPart(contentPartType);
option.RemoveHandler(handlerType);
}
internal ContentPartOption GetOrAddContentPart(Type contentPartType)
{
if (!contentPartType.IsSubclassOf(typeof(ContentPart)))
{
throw new ArgumentException("The type must inherit from " + nameof(ContentPart));
}
var option = _contentParts.FirstOrDefault(x => x.Type == contentPartType);
if (option == null)
{
option = new ContentPartOption(contentPartType);
_contentParts.Add(option);
}
return option;
}
internal void AddFieldHandler(Type contentFieldType, Type handlerType)
{
var option = GetOrAddContentField(contentFieldType);
option.AddHandler(handlerType);
}
View on GitHub (pinned to 4306c0717f)