dotnet / dotnet/aspnetcore

How to create custom metadata provider in .net core?

Open
#53,082 2 comments 0 reactions 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

I am trying to create a custom metadata provider in .NET 6 just like we have in .NET code given below

`[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class IsIdentifierAttribute : Attribute, IMetadataAware
{
public bool IsIdentifier { get; set; }

public IsIdentifierAttribute()
{
this.IsIdentifier = true;
}

public IsIdentifierAttribute(bool IsIdentifier)
{
this.IsIdentifier = IsIdentifier;
}

public void OnMetadataCreated(ModelMetadata metadata)
{
metadata.AdditionalValues["IsIdentifier"] = IsIdentifier;
}

}`

1- Is there any way to do this in .NET 6?
2- I want to get metadata for type given at runtime without using DI just like its being done in .NET code given below.

`ModelMetadataProviders.Current.GetMetadataForType(null, typeof(TEntity))`

Please provide your response on this.

Thanks,
Sohaib

### Expected Behavior

_No response_

### Steps To Reproduce

_No response_

### Exceptions (if any)

_No response_

### .NET Version

.NET 6

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.