问题
In an ASP .NET Web API using OData, I have a C# object describing the fields allowed to be used in $filter
Say I want to limit $filter
support to only $filter=deviceId gt 'someValue'
(followed http://www.ben-morris.com/parsing-odata-queries-decoupled-data-entities-webapi/ approach...).
So I defined a class like this:
[DataContract]
public class DeviceODataQuery
{
[DataMember(Name = "deviceId")]
public string DeviceId { get; set; }
}
Then I build myself the OData context because I can't use IQueryable
(too long to explain why, its legacy code...).
It looks like this:
/* Configure supported fields for query */
var builder = new ODataConventionModelBuilder();
builder.EntitySet<DeviceODataQuery>("DeviceODataQuery");
builder.Namespace = typeof(DeviceODataQuery).Namespace;
var model = builder.GetEdmModel();
ODataQueryContext = new ODataQueryContext(model, typeof(DeviceODataQuery));
/* Configure supported OData parameters for validation */
ODataValidationSettings.AllowedFunctions = AllowedFunctions.None;
ODataValidationSettings.AllowedLogicalOperators = AllowedLogicalOperators.GreaterThan;
ODataValidationSettings.AllowedArithmeticOperators = AllowedArithmeticOperators.None;
ODataValidationSettings.AllowedQueryOptions = AllowedQueryOptions.Top | AllowedQueryOptions.Filter;
Long story short, I can now use $filter=DeviceId gt 'someValue'
in my requests, but it rejects using the name from the annotation, I cannot use deviceId
...
It seems like the annotations are ignored, is there another way to do it or fix my code to make annotations working?
来源:https://stackoverflow.com/questions/33663255/odata-entity-property-serialization-name-different-than-c-sharp-variable-name