1

Could you help me please to minimize response of my Web API Route.

public class Product
{
    public string UniqueId {get;set;}
    public string Title {get;set;}
    ...
}

public class ProductsController : ApiController
{
    public IEnumerable<Product> GetAllProducts()
    {
        return repository.GetAll();
    }
    // ....
}

The response contains full names of the entity properties:

[{
    UniqueId: 123,
    Title: 'Book 1'
},...]

I would like to minimize traffic by using short aliases for DTO properties and see something like this:

[{
    u: 123,
    t: 'Book 1'
},...]

I'm wondering if special attributes could be used to rename properties in request/response. BTW I'm talking about requests because I have the same issue for POST requests.

1 Answer 1

4

Try applying the following attribute on your DTO's properties:

[JsonProperty(PropertyName = "u")]
public string UniqueId {get; set;}

In this way, JSON.NET will know which name to use when serializing or deserializing your DTOs.

Sign up to request clarification or add additional context in comments.

1 Comment

You can also do this without JSON.NET by adding [DataContract] attribute to your class and [DataMember(Name="u")] attribute to the property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.