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



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

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 属性的情况下解决此问题?

我正在将SelectExpandSelect设置为 null 从ODataQueryOptionsODataRawQueryOptions,但它们没有帮助:

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

我通过重置RequestUri解决了问题:

var requestUri = queryOptions.Request.RequestUri;
var uri = requestUri.AbsoluteUri.Substring(0, requestUri.AbsoluteUri.Length - requestUri.Query.Length);
queryOptions.Request.RequestUri = new Uri(uri);

最新更新