猿问

URI 中指定的查询无效。在类型上找不到名为“Name”的属性

当 odata 请求包含 $select 查询时,我返回不同的实体。这是我的获取方法:


  public IHttpActionResult Get(ODataQueryOptions<Product> queryOptions, CancellationToken cancellationToken)

  {

        if (Context == null)

            Context = MainContext.CreateInstance();

        var products = Context.GetEntities<Product>();

        if (queryOptions.RawValues?.Select != null)

        {

            var goodProducts = MakeGoodProductList(products); // Type of entity is GoodProduct

            return Ok(goodProducts);

        }

        return Ok(products);

   }

当请求时,我收到以下错误https://localhost:44326/Products?$select=Name


URI 中指定的查询无效。在“GoodProduct”类型上找不到名为“Name”的属性。


如何在GoodProduct类中不创建 Name 属性的情况下解决此问题?


我正在将SelectExpandand设置Select为 null ODataQueryOptions,ODataRawQueryOptions但它们没有帮助:


typeof(ODataQueryOptions).GetProperty(nameof(queryOptions.SelectExpand)).SetValue(queryOptions, null, null);

typeof(ODataRawQueryOptions).GetProperty(nameof(queryOptions.rawQueryOptions.Select)).SetValue(queryOptions.rawQueryOptions, null, null);


函数式编程
浏览 86回答 1
1回答

郎朗坤

我通过重置解决了问题RequestUri:var&nbsp;requestUri&nbsp;=&nbsp;queryOptions.Request.RequestUri;var&nbsp;uri&nbsp;=&nbsp;requestUri.AbsoluteUri.Substring(0,&nbsp;requestUri.AbsoluteUri.Length&nbsp;-&nbsp;requestUri.Query.Length); queryOptions.Request.RequestUri&nbsp;=&nbsp;new&nbsp;Uri(uri);
随时随地看视频慕课网APP
我要回答