{"record":{"id":"d0bc95aabee24a75","repo":"OrchardCMS/OrchardCore","slug":"failed-casting-content-to-typeof-telement-name-check-you","errorCode":null,"errorMessage":"Failed casting content to '{typeof(TElement).Name}', check you have registered your content part with AddContentPart?","messagePattern":"Failed casting content to '(.+?)', check you have registered your content part with AddContentPart\\?","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.ContentManagement.Abstractions/ContentExtensions.cs","lineNumber":47,"sourceCode":"    /// <param name=\"contentElement\">The <see cref=\"ContentElement\"/>.</param>\n    /// <param name=\"name\">The name of the content element.</param>\n    /// <typeparam name=\"TElement\">The expected type of the content element.</typeparam>\n    /// <returns>The content element instance or. <code>null</code> if it doesn't exist.</returns>\n    public static TElement Get<TElement>(this ContentElement contentElement, string name) where TElement : ContentElement\n    {\n        var result = contentElement.Get(typeof(TElement), name);\n\n        if (result == null)\n        {\n            return null;\n        }\n\n        if (result is TElement te)\n        {\n            return te;\n        }\n\n        throw new InvalidCastException($\"Failed casting content to '{typeof(TElement).Name}', check you have registered your content part with AddContentPart?\");\n    }\n\n    /// <summary>\n    /// Gets whether a content element has a specific element attached.\n    /// </summary>\n    /// <param name=\"contentElement\">The <see cref=\"ContentElement\"/>.</param>\n    /// <typeparam name=\"TElement\">The expected type of the content element.</typeparam>\n    public static bool Has<TElement>(this ContentElement contentElement) where TElement : ContentElement\n    {\n        return contentElement.Has(typeof(TElement).Name);\n    }\n\n    /// <summary>\n    /// Gets a content element by its name.\n    /// </summary>\n    /// <param name=\"contentElement\">The <see cref=\"ContentElement\"/>.</param>\n    /// <param name=\"contentElementType\">The expected type of the content element.</param>\n    /// <param name=\"name\">The name of the content element.</param>","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/ContentExtensions.cs#L29-L65","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before (no registration)\nvar part = contentItem.As<MyPart>(); // throws\n\n// after (Startup.cs)\nservices.AddContentPart<MyPart>();\n// usage\nvar part = contentItem.As<MyPart>();","handlingStrategy":"type-guard","validationCode":"if (!contentItem.Has(\"MyPart\")) return null;","typeGuard":"MyPart GetPartOrNull(ContentItem item) => item.As<MyPart>(); // plus registration check\nbool HasPart(ContentItem item) => item is not null && item.Has(nameof(MyPart));","tryCatchPattern":"try\n{\n    var part = contentItem.As<MyPart>();\n}\ncatch (InvalidCastException)\n{\n    // part not registered or wrong type; log and treat as absent\n}","preventionTips":["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."],"tags":["invalid-cast","content-part","orchardcore"],"backgroundTag":"type-mismatch","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"}