我的实体
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());
}
}
当我运行query / odata / A / 1?$ select = Name时,我得到json只有一个字段Name。 但是当我运行query / 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)"
}
请为此问题提出任何解决方案。
由于
答案 0 :(得分:1)
发现问题。 在我的实际代码中,C和D是字段,而不是属性。
将它们更改为属性解决了这个问题。
答案 1 :(得分:0)
HJ,
尝试以下方法......
/odata/A(1)?$select=b&$expand=b
希望这有帮助,
Ed Mendez