I am using OData v4 with Web API 2.2.
I have an Entity called "Person" with Composite keys of "FirstName" and "LastName". Looks like this:
public class Person {
public string FirstName {get; set;}
public string LastName {get; set;}
public double Age {get; set;}
}
In order to support composite keys, I have added a uri conventions on top of the default one, it looks like this:
public class CompositeKeyRoutingConvention : EntityRoutingConvention
{
public override string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup<string, HttpActionDescriptor> actionMap)
{
var action = base.SelectAction(odataPath, controllerContext, actionMap);
if (action != null)
{
var routeValues = controllerContext.RouteData.Values;
if (routeValues.ContainsKey(ODataRouteConstants.Key))
{
var keyRaw = (string)routeValues[ODataRouteConstants.Key];
var compoundKeyPairs = keyRaw.Split(',');
if (!compoundKeyPairs.Any())
{
return action;
}
foreach (var compoundKeyPair in compoundKeyPairs)
{
var pair = compoundKeyPair.Split('=');
if (pair.Length != 2)
{
continue;
}
var keyName = pair[0].Trim();
var keyValue = pair[1].Trim();
routeValues.Add(keyName, keyValue);
}
}
}
return action;
}
My calling code is trying to access the age of a person like so:
http://localhost:46028/Person(firstName='Blah',LastName='Blu')/Age
I get this error:
{ "error":{ "code":"","message":"No HTTP resource was found that matches the request URI 'http://:46028/Person(firstName='Blah',LastName='Blu')/Age'.","innererror":{ "message":"No routing convention was found to select an action for the OData path with template '~/entityset/key/property'.","type":"","stacktrace":"" } } }
my controller has two methods:
public IQueryable<Person> Get()
{
return _db.People;
}
public Person Get([FromODataUri] string firstName, [FromODataUri] string lastName)
{
var person = _db.People
.FirstOrDefault(x => x.FirstName == firstName && x.LastName== lastName);
if (person == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
return person;
}