Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying custom property name when binding object to Web API endpoint

I have a .Net Core Web API. It automatically maps models when the model properties match the request body. For example, if you have this class:

public class Package {     public string Carrier { get; set; }     public string TrackingNumber { get; set; } } 

It would correctly bind it to a POST endpoint if the request body is the following JSON:

{     carrier: "fedex",     trackingNumber: "123123123" } 

What I need to do is specify a custom property to map. For example, using the same class above, I need to be able to map to JSON if the TrackingNumber comes in as tracking_number.

How do I do that?

like image 838
im1dermike Avatar asked Mar 17 '17 00:03

im1dermike


People also ask

How do I pass a class object as parameter in Web API?

Show activity on this post. MakeUser method in the User controller for creating a username and password. UserParameter class for sending the parameters as an object. RunAsync method in console client.

How do we do parameter binding in Web API?

The item parameter is a complex type, so Web API uses a media-type formatter to read the value from the request body. To get a value from the URI, Web API looks in the route data and the URI query string. The route data is populated when the routing system parses the URI and matches it to a route.

What is FromBody and FromUri in Web API?

The [FromUri] attribute is prefixed to the parameter to specify that the value should be read from the URI of the request, and the [FromBody] attribute is used to specify that the value should be read from the body of the request.

What is difference between FromForm and FromBody?

[FromForm] - Gets values from posted form fields. [FromBody] - Gets values from the request body.


2 Answers

TejSoft's answer does not work in ASP.NET Core 3.0 Web APIs by default.

Starting in 3.0, the ASP.NET Core Json.NET (Newtonsoft.Json) sub-component is removed from the ASP.NET Core shared framework. It is announced that, "Json.NET will continue to work with ASP.NET Core, but it will not be in the box with the shared framework." The newly added Json Api claimed to be specifically geared for high-performance scenarios.

Use JsonPropertyName attribute to set a custom property name:

using System.Text.Json.Serialization;  public class Package {     [JsonPropertyName("carrier")]     public string Carrier { get; set; }      [JsonPropertyName("tracking_number")]     public string TrackingNumber { get; set; } } 
like image 73
Mert Can Ilis Avatar answered Sep 17 '22 17:09

Mert Can Ilis


Change your package class and add JsonProperty decoration for each field you wish to map to a different json field.

public class Package {     [JsonProperty(PropertyName = "carrier")]     public string Carrier { get; set; }      [JsonProperty(PropertyName = "trackingNumber")]     public string TrackingNumber { get; set; } } 
like image 35
TejSoft Avatar answered Sep 21 '22 17:09

TejSoft