简体   繁体   中英

AssociatedMetadataProvider in ASP.NET Core

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();

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:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM