ASP.NET Core 6.0中的DataAnnotationsModelMetadataProvider

yc0p9oo0  于 2023-08-08  发布在  .NET
关注(0)|答案(1)|浏览(100)

我正在将一个项目从ASP.NET MVC迁移到ASP.NET Core 6.0。
我在ASP.NET MVC项目中有以下类:

public class CustomModelMetadataProvider : DataAnnotationsModelMetadataProvider
{
    protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
    {
        var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);

        var textAreaAttribute = attributes.SingleOrDefault(a => typeof(TextAreaAttribute) == a.GetType());

        if (textAreaAttribute != null)
        {
            metadata.AdditionalValues.Add("TextArea", null);
            metadata.AdditionalValues.Add("Rows", ((TextAreaAttribute)textAreaAttribute).Rows);
            metadata.AdditionalValues.Add("Columns", ((TextAreaAttribute)textAreaAttribute).Columns);
            metadata.AdditionalValues.Add("MaxLength", ((TextAreaAttribute)textAreaAttribute).MaxLength);
            metadata.AdditionalValues.Add("Placeholder", ((TextAreaAttribute)textAreaAttribute).Placeholder);
        }

        var textInputAttribute = attributes.SingleOrDefault(a => typeof(TextInputAttribute) == a.GetType());

        if (textInputAttribute != null)
        {
            metadata.AdditionalValues.Add("TextInput", null);
            metadata.AdditionalValues.Add("MaxLength", ((TextInputAttribute)textInputAttribute).MaxLength);
        }

        return metadata;
    }
}

字符串
在ASP.NET Core 6.0中与此等效的是什么?
一个使用案例:

[TextInput(MaxLength = 10)] // limit number of field input characters to 10


文本输入:

public class TextInputAttribute : Attribute
{
    public int MaxLength { get; set; }

    public Dictionary<string, object> OptionalAttributes()
    {
        return new Dictionary<string, object>
        {
            { "MaxLength", MaxLength }
        };
    }
}


在ASP.NETMVC项目Global.asax文件中设置了以下内容:

ModelMetadataProviders.Current = new CustomModelMetadataProvider();

wtlkbnrh

wtlkbnrh1#

.net内核中没有DataAnnotationsModelMetadataProvider,只有IModelMetadataProvider。你可能注意到它提供DefaultModelMetadataProvider作为基类:

public class MyModelMetadataProvider : DefaultModelMetadataProvider
{
    public MyModelMetadataProvider(ICompositeMetadataDetailsProvider detailsProvider) : base(detailsProvider)
    {
    }

    protected override DefaultMetadataDetails[] CreatePropertyDetails(ModelMetadataIdentity key)
    {
        var defaultPropertyDetails = base.CreatePropertyDetails(key);

        var attributes = defaultPropertyDetails[0].ModelAttributes.PropertyAttributes;

        var metadata = base.CreateModelMetadata(defaultPropertyDetails[0]);

        var textInputAttribute = attributes.SingleOrDefault(a => typeof(TextInputAttribute) == a.GetType());

        if (textInputAttribute != null)
        {
            //do something
        }

        return defaultPropertyDetails;
    }
}

字符串
但在DefaultModelMetadataProvider中,AdditionalValues是只读属性,因此根据代码,替代选项应该是IDisplayMetadataProvider

public class AdditionalMetadataValuesProvider : IDisplayMetadataProvider
{
    public void CreateDisplayMetadata(DisplayMetadataProviderContext context)
    {
        foreach (var attribute in context.Attributes.OfType<AdditionalMetadataValueAttribute>())
        {
            context.DisplayMetadata.AdditionalValues.Add(attribute.Key, attribute.Value);
        }
    }
}


在Program.cs中注册:

builder.Services.AddSingleton<IDisplayMetadataProvider, AdditionalMetadataValuesProvider>();


有关更多详细信息,请参阅this link

相关问题