Try our conversational search powered by Generative AI!

Issue after upgrading to latest stable versions of EpiServer nuget packages

Vote:
 

Hello,

After upgrading to all Episerver nuget packages to latest stable versions of CMS12, we are seeing a casting issue:

      Message=Unable to cast object of type 'Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.DisplayMetadata' to type 'EPiServer.Shell.ObjectEditing.ExtendedMetadata'.

Sample Code:

namespace KK.Opti.Common.Menu.Infrastructure.Attributes
{
    public class PropertyListLinkAttribute : Attribute, IMenuModelMetadataAware
    {
        public Injected<IUrlResolver> UrlResolver { get; set; }
        public Dictionary<string, string> LinkMappings { get; set; }

        public virtual void OnMetadataCreated(DisplayMetadata metadata)
        {
            var extendedMetadata = (ExtendedMetadata)metadata;

            var list = ((MenuItemProperty)extendedMetadata.Model).List;

       }

Any help is appreciated.

#294612
Edited, Jan 13, 2023 0:31
Vote:
 

Shot in the dark here, try removing the interface and include the following using 

    using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;

After some reading 

"One thing that has been removed in MVC6 is the IMetadataAware interface. This allowed you to create your own subclasses of Attribute that could add data to the ModelMetadata structure to be used in an EditorTemplate or DisplayTemplate view." 

See more info here : Reinstating IMetadataAware in ASP.NET 5 vNext MVC 6 (emikek.com) 

#294614
Jan 13, 2023 1:51
Vote:
 

We are doing exactly how that link suggests. The code was working until we just updated EpiServer nuget pkgs to latest stable versions.

#294634
Jan 13, 2023 16:58
Vote:
 

Hi Manish,

I'm not sure if you have resolved your case, but I ran into a similar issue and have been able to figure out a different approach for this. Instead of using the attribute you can create a custom Editor Descriptor that inherits from the built in CollectionEditorDescriptor and override the ModifyMetadata method which provides an ExtendedMetadata property. Then remove the attribute and update the Editor Descriptor type on the property to use this new one instead of the built in one. Below is the code I used to resolve the issue.

public class PropertyListLinkCollectionEditorDescriptor : CollectionEditorDescriptor<MenuItem>
    {
        private readonly IUrlResolver _urlResolver;

        public PropertyListLinkCollectionEditorDescriptor(IUrlResolver urlResolver)
        {
            _urlResolver = urlResolver;
        }

        public Dictionary<string, string> LinkMappings { get; set; }

        public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes)
        {
            base.ModifyMetadata(metadata, attributes);

            var list = ((MenuItemProperty)metadata.Model).List;

            if (list != null)
            {
                LinkMappings = new Dictionary<string, string>();

                SetLinkMappings(list);
                metadata.EditorConfiguration.Add("mappedLinks", LinkMappings);
            }
        }

        public void SetLinkMappings<TMenuItem>(IEnumerable<TMenuItem> collection) where TMenuItem : IMenuItem<TMenuItem>
        {
            foreach (var item in collection)
            {
                if (item.Link != null && !LinkMappings.ContainsKey(item.Link.ToString()))
                {
                    LinkMappings.Add(item.Link.ToString(), _urlResolver.GetUrl(new UrlBuilder(item.Link), ContextMode.Default));
                }

                if (item.MenuItems.Any())
                {
                    SetLinkMappings(item.MenuItems);
                }
            }
        }
    }
#299589
Apr 05, 2023 15:38
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.