使用Web API 2.0时在过滤器上获取错误

时间:2014-02-25 21:09:40

标签: asp.net-web-api webapp2 asp.net-web-api2

对于以下REST查询,我收到错误。(它适用于分页/排序) 我该如何修复此错误。

http://localhost:18376/api/Org/GetOrgHeadQuarter?%24inlinecount=allpages&%24top=2&%24filter=Name+eq+%27test%27

这是WebAPI 2.1方法。

public PageResult<dynamic> GetOrgHeadQuarter(ODataQueryOptions options)
    {
        try
        {


            IQueryable results = options.ApplyTo(db.OrgHeadQuarters
                                   .Select(os => new 
                                   {
                                       ROWID = os.ROWID,
                                       OrgHeadCode_PK = os.OrgHeadCode_PK,
                                       Code = os.Code,
                                       Name = os.Name,
                                       Description = os.Description,
                                       Address1 = os.Address1,
                                       Address2 = os.Address2,
                                       CountryCode_FK = os.CountryCode_FK,
                                       EffectiveDate = os.EffectiveDate,
                                       TerminationDate = os.TerminationDate,
                                       DateCreated = os.DateCreated,
                                       DateUpdated = os.DateUpdated,
                                       CreatedByUserID_FK = os.CreatedByUserID_FK,
                                       UpdatedByUserID_FK = os.UpdatedByUserID_FK,
                                       CountryName = os.Country.CountryName,
                                       StateCode_FK = os.StateCode_FK,
                                       StateName = os.State.StateName,
                                       CityCode_FK = os.CityCode_FK,
                                       CityName = os.City.CityName,
                                       ZIP = os.ZIP,
                                       RowStatusID_FK = os.RowStatusCode_FK,
                                       RowStatusName = os.RowStatus.Name,
                                       CreatedByUserName = os.CreatedByUser.UserName,
                                       UpdatedByUserName = os.UpdatedByUser.UserName
                                   }).OrderBy(o => o.OrgHeadCode_PK));


        return new PageResult<dynamic>(
            results as IEnumerable<dynamic>,
            Request.GetNextPageLink(),
            Request.GetInlineCount());

        }
        catch (Exception e)
        {
            throw;
        }
    }

这是我得到的错误。

{ "Message": "An error has occurred.", "ExceptionMessage": "Could not find a property named 'Name' on type 'System.Object'.", "ExceptionType": "Microsoft.Data.OData.ODataException", "StackTrace": " at Microsoft.Data.OData.Query.EndPathBinder.GeneratePropertyAccessQueryForOpenType(EndPathToken endPathToken, SingleValueNode parentNode)\r\n at Microsoft.Data.OData.Query.EndPathBinder.BindEndPath(EndPathToken endPathToken, BindingState state)\r\n at Microsoft.Data.OData.Query.MetadataBinder.BindEndPath(EndPathToken endPathToken)\r\n at Microsoft.Data.OData.Query.MetadataBinder.Bind(QueryToken token)\r\n at Microsoft.Data.OData.Query.BinaryOperatorBinder.GetOperandFromToken(BinaryOperatorKind operatorKind, QueryToken queryToken)\r\n at Microsoft.Data.OData.Query.BinaryOperatorBinder.BindBinaryOperator(BinaryOperatorToken binaryOperatorToken)\r\n at Microsoft.Data.OData.Query.MetadataBinder.BindBinaryOperator(BinaryOperatorToken binaryOperatorToken)\r\n at Microsoft.Data.OData.Query.MetadataBinder.Bind(QueryToken token)\r\n at Microsoft.Data.OData.Query.FilterBinder.BindFilter(QueryToken filter)\r\n at Microsoft.Data.OData.Query.ODataUriParser.ParseFilterImplementation(String filter, IEdmType elementType, IEdmEntitySet entitySet)\r\n at Microsoft.Data.OData.Query.ODataUriParser.ParseFilter(String filter, IEdmType elementType, IEdmEntitySet entitySet)\r\n at Microsoft.Data.OData.Query.ODataUriParser.ParseFilter(String filter, IEdmModel model, IEdmType elementType)\r\n at System.Web.Http.OData.Query.FilterQueryOption.get_FilterClause()\r\n at System.Web.Http.OData.Query.FilterQueryOption.ApplyTo(IQueryable query, ODataQuerySettings querySettings, IAssembliesResolver assembliesResolver)\r\n at System.Web.Http.OData.Query.ODataQueryOptions.ApplyTo(IQueryable query, ODataQuerySettings querySettings)\r\n at System.Web.Http.OData.Query.ODataQueryOptions`1.ApplyTo(IQueryable query, ODataQuerySettings querySettings)\r\n at System.Web.Http.OData.Query.ODataQueryOptions.ApplyTo(IQueryable query)\r\n at System.Web.Http.OData.Query.ODataQueryOptions`1.ApplyTo(IQueryable query)\r\n at SchoolManagement.HTML5.Controllers.OrgController.GetOrgHeadQuarter(ODataQueryOptions options) in c:\\Projects\\AuthProj\\Controllers\\OrgController.cs:line 160\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.b__9(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at System.Web.Http.Filters.AuthorizationFilterAttribute.d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at System.Web.Http.Controllers.AuthenticationFilterResult.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()" }`enter code here`

0 个答案:

没有答案