dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.5k stars 10.04k forks source link

How to create custom metadata provider in .net core? #53082

Open sohaibameenpk007 opened 10 months ago

sohaibameenpk007 commented 10 months ago

Is there an existing issue for this?

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

davidfowl commented 10 months ago

Doesn’t exist in .NET 6+. Model metadata is an ASP.NET Core MVC concept. What are you trying to do?

sohaibameenpk007 commented 10 months ago

I am trying to get model metadata in .NET 6 Core MVC just like I have mentioned in the legacy code which I have posted before.

How can I create custom metadata and get it in .NET core?

On Mon, 1 Jan 2024 at 9:51 PM, David Fowler @.***> wrote:

Doesn’t exist in .NET 6+. Model metadata is an ASP.NET Core MVC concept. What are you trying to do?

— Reply to this email directly, view it on GitHub https://github.com/dotnet/aspnetcore/issues/53082#issuecomment-1873397955, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVCWODNRT76HFEV5Q2KESWLYMLSQPAVCNFSM6AAAAABBJDQF5GVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNZTGM4TOOJVGU . You are receiving this because you authored the thread.Message ID: @.***>