简体   繁体   中英

Case-Insensitive Property Binding Without Using JsonProperty in .NET Core

I have a C# ASP.NET Core Web Api app and C# ASP.NET Core MVC app

In this case, the MVC app calls the API app with a model like this (the client was generated via AutoRest ):

var thing = await thingApi.ThingPost(new ThingModel { ThingID = ThingID });

At this point, I can debug to see that the ThingID is set with my value. When I debug the API app I can see that the property did not transfer over... property defined as such:

public long ThingID { get; set; }

However, when I decorate it with

[JsonProperty(PropertyName = "thingID")]

Then the value is bound. I am trying to figure out how I can have it bind without having to decorate it with that attribute. Some kind of a case-insensitive resolver or something?

I've tried using:

.AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());

and

.AddJsonOptions(options => options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver());

Without updating the client, both failed to resolve the property. The thing is that I don't want the client to change. It should be generated as it is now, but I want the value to be bound to the property regardless of the case in the client. How can I do this?

I found the culprit... my model inherited from a base class (which I probably should have mentioned in my question) and my base class was decorated with:

[DataContract]

which obviously forces you to specify each of the properties you want exposed... so a bit of a duh moment.

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