私のエンティティ
public class A
{
public int id {get;set;}
public string Name {get;set;}
public List<B> b {get;set;}
}
public class B
{
public string C {get;set;}
public string D {get;set;}
}
My controller class
[RoutePrefix("odata/A/{id}")]
[ODataRouting]
public class AsController : ApiController
{
[Queryable]
public async Task<HttpResponseMessage> GetA(int id)
{
// here i call another method that gives me List<A> result
HttpResponseMessage httpResponseMessage = Request.CreateResponse(HttpStatusCode.OK, result);
return httpResponseMessage;
}
}
WebApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Filters.Add(new ValidationActionFilter());
// Web API configuration and services
config.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling =
TypeNameHandling.Objects;
var contractResolver = (config.Formatters.JsonFormatter.SerializerSettings.ContractResolver as DefaultContractResolver);
contractResolver.IgnoreSerializableAttribute = true;
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<A>("As");
builder.EntitySet<B>("b");
config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());
}
}
クエリ /odata/A/1?$select=Name を実行すると、1 つのフィールド Name のみを持つ json が取得されます。しかし、クエリ /odata/A/1?$select=b を実行すると、例外で出力が得られます
{
"$type":"System.Web.Http.HttpError, System.Web.Http",
"Message":"The query specified in the URI is not valid.",
"ExceptionMessage":"Could not find a property named 'b' on type 'A'.",
"ExceptionType":"Microsoft.Data.OData.ODataException",
"StackTrace":" at Microsoft.Data.OData.Query.SyntacticAst.SelectPathSegmentTokenBinder.ConvertNonTypeTokenToSegment(PathSegmentToken tokenIn, IEdmModel model, IEdmEntityType entityType)
at Microsoft.Data.OData.Query.SyntacticAst.SelectPropertyVisitor.ProcessTokenAsPath(NonSystemToken tokenIn)
at Microsoft.Data.OData.Query.SyntacticAst.SelectPropertyVisitor.Visit(NonSystemToken tokenIn)
at Microsoft.Data.OData.Query.SyntacticAst.NonSystemToken.Accept(IPathSegmentTokenVisitor visitor)
at Microsoft.Data.OData.Query.SyntacticAst.SelectBinder.Bind(SelectToken tokenIn)
at Microsoft.Data.OData.Query.SelectExpandSemanticBinder.Parse(IEdmEntityType elementType, IEdmEntitySet entitySet, ExpandToken expandToken, SelectToken selectToken, ODataUriParserConfiguration configuration)
at Microsoft.Data.OData.Query.ODataUriParser.ParseSelectAndExpandImplementation(String select, String expand, IEdmEntityType elementType, IEdmEntitySet entitySet)
at Microsoft.Data.OData.Query.ODataUriParser.ParseSelectAndExpand(String select, String expand, IEdmEntityType elementType, IEdmEntitySet entitySet)
at System.Web.Http.OData.Query.SelectExpandQueryOption.get_SelectExpandClause()
at System.Web.Http.OData.Query.Validators.SelectExpandQueryValidator.Validate(SelectExpandQueryOption selectExpandQueryOption, ODataValidationSettings validationSettings)
at System.Web.Http.OData.Query.SelectExpandQueryOption.Validate(ODataValidationSettings validationSettings)
at System.Web.Http.OData.Query.Validators.ODataQueryValidator.Validate(ODataQueryOptions options, ODataValidationSettings validationSettings)
at System.Web.Http.OData.Query.ODataQueryOptions.Validate(ODataValidationSettings validationSettings)
at System.Web.Http.QueryableAttribute.ValidateQuery(HttpRequestMessage request, ODataQueryOptions queryOptions)
at System.Web.Http.QueryableAttribute.ExecuteQuery(Object response, HttpRequestMessage request, HttpActionDescriptor actionDescriptor)
at System.Web.Http.QueryableAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)"
}
この問題の解決策を提案してください。
ありがとう