URI 中指定的查询无效。无法在类型上找到名为 'Name' 的 属性
The query specified in the URI is not valid. Could not find a property named 'Name' on type
当 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
时出现以下错误
The query specified in the URI is not valid. Could not find a property named 'Name' on type 'GoodProduct'.
如何在不在 GoodProduct
class 中创建名称 属性 的情况下解决此问题?
我正在将 SelectExpand
和 Select
从 ODataQueryOptions
和 ODataRawQueryOptions
设置为 null,但它们没有帮助:
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);
当 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
The query specified in the URI is not valid. Could not find a property named 'Name' on type 'GoodProduct'.
如何在不在 GoodProduct
class 中创建名称 属性 的情况下解决此问题?
我正在将 SelectExpand
和 Select
从 ODataQueryOptions
和 ODataRawQueryOptions
设置为 null,但它们没有帮助:
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);