OrchardCMS/OrchardCore · error · InvalidCastException
Failed casting content to
Error message
Failed casting content to '{typeof(TElement).Name}', check you have registered your content part with AddContentPart? What it means
ContentExtensions.Get<TElement> throws InvalidCastException when the part/field attached to the content element is not of the requested type TElement. OrchardCore stores parts as typed elements only if the part type was registered; otherwise the stored element cannot be cast to TElement.
Solutions
- Register the part in your module Startup: services.AddContentPart<MyPart>(); (or AddContentPart<T, TDisplayDriver> etc.).
- Verify the content item actually has a part/field of that type: use item.Has("MyPart") before calling As<T>().
- Check the stored part type name matches the class name; if the class was renamed, migrate existing content definitions.
- Use Get(typeof(TElement)) inspection or item.Parts to see what is actually attached and fix the requested type.
Example fix
// before (no registration) var part = contentItem.As<MyPart>(); // throws // after (Startup.cs) services.AddContentPart<MyPart>(); // usage var part = contentItem.As<MyPart>();
Defensive patterns
Strategy: type-guard
Validate before calling
if (!contentItem.Has("MyPart")) return null; Type guard
MyPart GetPartOrNull(ContentItem item) => item.As<MyPart>(); // plus registration check bool HasPart(ContentItem item) => item is not null && item.Has(nameof(MyPart));
Try / catch
try
{
var part = contentItem.As<MyPart>();
}
catch (InvalidCastException)
{
// part not registered or wrong type; log and treat as absent
} Prevention
- Register every content part with services.AddContentPart<T>() in module Startup.
- Use item.Has(name) before As<T>() in defensive code paths.
- Never rename part classes without a migration updating stored definitions.
- Keep part and field types distinct; use Get<TField> for fields.
When it happens
Trigger: Calling item.As<MyPart>() (or Get<MyPart>) when MyPart was never registered via services.AddContentPart<MyPart>(), or when the content item actually has a different part/field under that name, or casting a field to a part type (or vice versa).
Common situations: Forgot to register the part in a module Startup.cs, renamed/moved the part class so the stored type name no longer matches, querying a ContentField with Get<TPart>, typo in part name for a different type.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Cannot convert to Int16
- Cannot convert to Int32
- Cannot convert to Int64
- Cannot convert to SByte
- Cannot convert to Float
AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13).
Data as JSON: /api/errors/d0bc95aabee24a75.
Report an issue: GitHub.
Appendix: source
Thrown at src/OrchardCore/OrchardCore.ContentManagement.Abstractions/ContentExtensions.cs:47
/// <param name="contentElement">The <see cref="ContentElement"/>.</param>
/// <param name="name">The name of the content element.</param>
/// <typeparam name="TElement">The expected type of the content element.</typeparam>
/// <returns>The content element instance or. <code>null</code> if it doesn't exist.</returns>
public static TElement Get<TElement>(this ContentElement contentElement, string name) where TElement : ContentElement
{
var result = contentElement.Get(typeof(TElement), name);
if (result == null)
{
return null;
}
if (result is TElement te)
{
return te;
}
throw new InvalidCastException($"Failed casting content to '{typeof(TElement).Name}', check you have registered your content part with AddContentPart?");
}
/// <summary>
/// Gets whether a content element has a specific element attached.
/// </summary>
/// <param name="contentElement">The <see cref="ContentElement"/>.</param>
/// <typeparam name="TElement">The expected type of the content element.</typeparam>
public static bool Has<TElement>(this ContentElement contentElement) where TElement : ContentElement
{
return contentElement.Has(typeof(TElement).Name);
}
/// <summary>
/// Gets a content element by its name.
/// </summary>
/// <param name="contentElement">The <see cref="ContentElement"/>.</param>
/// <param name="contentElementType">The expected type of the content element.</param>
/// <param name="name">The name of the content element.</param>View on GitHub (pinned to 4306c0717f)