AssociatedMetadataProvider in ASP.NET Core

600 Views Asked by At

Where I can find AssociatedMetadataProvider in .Net Core? Or is there a way to implement GetTypeDescriptor in DefaultModelMetadataProvider?

In MVC 5 the DataAnnotationsModelMetadataProvider inherits from AssociatedMetadataProvider which had a method GetTypeDescriptor.

It seems this class (AssociatedMetadataProvider ) was completly removed :-(

Any advice would be very nice.


How I did it in Mvc 5 ?

    public class CustomMetadataProvider : DataAnnotationsModelMetadataProvider
{
    protected override ICustomTypeDescriptor GetTypeDescriptor(Type type)
    {
        if (WhatEver)
        { return new MyICustomTypeDescriptor();}
        return base.GetTypeDescriptor(type);
    }
}

Registration:

Global.asax

 ModelMetadataProviders.Current = new CustomMetadataProvider();
1

There are 1 best solutions below

1
Vitalii Isaenko On

Indeed, there is no AssociatedMetadataProvider in .Net Core. However, they left us IModelMetadataProvider with abstract class ModelMetadataProvider that you might use, especially if you need to override some of the methods. You might also notice there is an implementation provided DefaultModelMetadataProvider that has a method GetMetadataForType(Type). It will return ModelMetadata that is still not what you need. But this class has a method called GetModelExplorerForType() and it returns an instance of ModelExplorer. This instance should have all the information you need in a bit different format.
I hope it helps :)