微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Microsoft.OData.ODataException-URI中指定的查询无效在类型上找不到名为“ tag”的属性

如何解决Microsoft.OData.ODataException-URI中指定的查询无效在类型上找不到名为“ tag”的属性

我有一个 get 端点请求- endpt {id} ,该请求根据传递的integer id检索表行。因此,认情况下,所有列均从表中返回。

  • 列表项

我只想返回表中总列的一个子集。因此,我正在尝试使用 oData $select 查询

{baseurl}/endpoint/3?$select=tag

其中 Tag 是列名之一。

在我的startup.cs文件中,Confiure方法已通过这种方式注册了oData。

enter image description here

我的获取端点。

    [HttpGet]
    [EnableQuery]
    [ODaTaroute("endpoint({id})")]
    [Route("endpt",Name = "endpt",Order = 0)]
    [Route("endpt/{id}",Order = 1)]        
    [ProducesResponseType(typeof(APIResponseModel<List<EndPoints>>),StatusCodes.Status200OK)]      
    public APIResponseviewmodel<List<EndPoints>> GetEndPoints(int limit,[FromODataUri] int id)
    {
        List<EndPoints> listofEndPoints;
        try
        {
            if (id.Equals(0))
            {
                listofEndPoints = db.EndPoints.ToList();
            }
            else
            {
                listofEndPoints = db.EndPoints.Where(x => x.ID.Equals(id)).ToList();                    
            }               
        }
        catch (Exception e)
        {
           //catch exp
        }

        if (limit == 0)
        {           

            this.HttpContext.Response.StatusCode = (int)HttpStatusCode.OK;

            return new APIResponseModel<List<EndPoints>>
            {
                StatusCode = (int)HttpStatusCode.OK,Message = "Success",Result = listofEndPoints,Count = listofEndPoints.Count
            };
        }
        else
        {           

            this.HttpContext.Response.StatusCode = (int)HttpStatusCode.OK;

            return new APIResponseModel<List<EndPoints>>
            {
                StatusCode = (int)HttpStatusCode.OK,Result = listofEndPoints.Take(limit).ToList(),Count = listofEndPoints.Count
            };
        }
    }

断点命中,但在 id 中,我仅获得所传递的 ID-3 。 select标签不会出现,并且从实现逻辑中,它返回与所有列匹配的 id 完整行。

但是在客户端,我从oDataUriParser收到一个错误

    {
    "Message": "The query specified in the URI is not valid. Could not find a property named 'tag' on type 'project'.","ExceptionMessage": "Could not find a property named 'tag' on type 'Projects'.","ExceptionType": "Microsoft.OData.ODataException","StackTrace": "   at Microsoft.OData.UriParser.SelectPathSegmentTokenBinder.ConvertNonTypetokenToSegment(PathSegmentToken tokenIn,IEdmModel model,IEdmStructuredType edmType,ODataUriResolver resolver,BindingState state)\r\n   at Microsoft.OData.UriParser.SelectExpandBinder.ProcessSelectTokenPath(PathSegmentToken tokenIn)\r\n   at Microsoft.OData.UriParser.SelectExpandBinder.GenerateSelectItem(SelectTermToken tokenIn)\r\n   at Microsoft.OData.UriParser.SelectExpandBinder.Bind(ExpandToken expandToken,SelectToken selectToken)\r\n   at Microsoft.OData.UriParser.SelectExpandSemanticBinder.Bind(ODataPathInfo odataPathInfo,ExpandToken expandToken,SelectToken selectToken,ODataUriParserConfiguration configuration,BindingState state)\r\n   at Microsoft.OData.UriParser.ODataQueryOptionParser.ParseSelectAndExpandImplementation(String select,String expand,ODataPathInfo odataPathInfo)\r\n   at Microsoft.OData.UriParser.ODataQueryOptionParser.ParseSelectAndExpand()\r\n   at Microsoft.AspNet.OData.Query.SelectExpandQueryOption.get_SelectExpandClause()\r\n   at Microsoft.AspNet.OData.Query.Validators.SelectExpandQueryValidator.Validate(SelectExpandQueryOption selectExpandQueryOption,ODataValidationSettings validationSettings)\r\n   at Microsoft.AspNet.OData.Query.SelectExpandQueryOption.Validate(ODataValidationSettings validationSettings)\r\n   at Microsoft.AspNet.OData.Query.Validators.ODataQueryValidator.Validate(ODataQueryOptions options,ODataValidationSettings validationSettings)\r\n   at Microsoft.AspNet.OData.Query.ODataQueryOptions.Validate(ODataValidationSettings validationSettings)\r\n   at Microsoft.AspNet.OData.EnableQueryAttribute.ValidateQuery(HttpRequest request,ODataQueryOptions queryOptions)\r\n   at Microsoft.AspNet.OData.EnableQueryAttribute.CreateAndValidateQueryOptions(HttpRequest request,ODataQueryContext queryContext)\r\n   at Microsoft.AspNet.OData.EnableQueryAttribute.<>c__displayClass1_0.<OnActionExecuted>b__1(ODataQueryContext queryContext)\r\n   at Microsoft.AspNet.OData.EnableQueryAttribute.ExecuteQuery(Object responseValue,IQueryable singleResultCollection,IWebApiActionDescriptor actionDescriptor,Func`2 modelFunction,IWebApiRequestMessage request,Func`2 createqueryOptionFunction)\r\n   at Microsoft.AspNet.OData.EnableQueryAttribute.OnActionExecuted(Object responseValue,Func`2 createqueryOptionFunction,Action`1 createResponseAction,Action`3 createErrorAction)"
}

请帮助我解决此问题,因为我的URL没有任何复杂性。但我找不到丢失的点。

谢谢

解决方法

您可以尝试更改此设置:

oDataBuilder.EntitySet<Endpoints>("Endpoints");

为此:

oDataBuilder.EntitySet<Endpoint>("Endpoints");

这个想法是你的模型被称为 Endpoint,你的控制器被称为 Endpoints

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。