在属性上添加过滤器以进行odata查询 [英] Add filter on property for odata query
本文介绍了在属性上添加过滤器以进行odata查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个实体:ItContract,每个ItContract都属于一个组织单位.
I have an entity: ItContract and every ItContract belongs to an organisation unit.
用户登录到组织单位并具有对所有数据的读取访问权限. 如何为每个odata查询在服务器上的OrganisationUnitId上设置过滤器?
A user logs-in to an organisation unit and have read access to all data. How can I set a filter on organisationUnitId on the server for every odata query?
我正在使用带asp.net的odata v4.
I am using odata v4 with asp.net.
推荐答案
有一种方法可以覆盖在服务器端获得的queryOption.
There is a way to override the queryOption you get in server side.
public IHttpActionResult Get(ODataQueryOptions<People> queryOptions)
{
// get the original request before the alterations
HttpRequestMessage originalRequest = queryOptions.Request;
// get the original URL before the alterations
string url = originalRequest.RequestUri.AbsoluteUri;
// rebuild the URL
if (queryOptions.Filter != null)
{
// apply the new filter
url = url.Replace("$filter=", "$filter=organisationUnitId%20eq%20" + organisationUnitId + ",");
}
else
{
if (url.Contains("$"))
{
url += "&";
}
url += "$filter=organisationUnitId%20eq%20" + organisationUnitId;
}
// build a new request for the filter
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, url);
// reset the query options with the new request
queryOptions = new ODataQueryOptions(queryOptions.Context, req);
var result = queryOptions.ApplyTo(_db.Prople);
return Ok(result, result.GetType());
}
private IHttpActionResult Ok(object content, Type type)
{
var resultType = typeof(OkNegotiatedContentResult<>).MakeGenericType(type);
return Activator.CreateInstance(resultType, content, this) as IHttpActionResult;
}
这篇关于在属性上添加过滤器以进行odata查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文