Reputation: 2804
I am using Newtonsoft to change property names on the web api json output.
public class User : IEntity
{
[Newtonsoft.Json.JsonProperty(PropertyName = "user_name"]
public string Username { get; set; }
}
I've enabled odata query so that i can add queries to the request.
[HttpGet]
[Route("api/users")]
[EnableQuery]
public IQueryable<User> GetUser()
{
return dbContext.DbSet<User>();
}
When i make a query using the alternate property name, it fails.
GET /api/users?$select=user_name
The query specified in the URI is not valid. Could not find a property named 'user_name'
The query works fine if I use the entity model name, Username
(which is not visible to public). How can i fix this while still using Newtonsoft to handle deserialization?
Upvotes: 5
Views: 4096
Reputation: 22465
I've not been able to achive this with the Json.Property attribute but instead this way:
// in class WepApiConfig
ODataModelBuilder builder = new ODataConventionModelBuilder();
var conf = builder.EntitySet<User>("users");
conf.EntityType.Property(f => f.Username).Name = "user_name";
the query
GET /api/users?$select=user_name
should work now (with my OData service it works fine)
My answer is based on the reply to this question but with minor corrections.
Upvotes: 7