1

私のエンティティ

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)"
}

この問題の解決策を提案してください。

ありがとう

4

2 に答える 2

1

問題が見つかりました。私の実際のコードでは、C と D はフィールドであり、プロパティではありません。

それらをプロパティに変更すると、問題が解決しました。

于 2014-09-09T10:17:24.573 に答える
0

hj、

次のことを試してください...

/odata/A(1)?$select=b&$expand=b

お役に立てれば、

エド・メンデス

于 2014-08-21T18:48:22.983 に答える