如何阅读在C#中的行动OData的URL参数值?

问题描述 投票:0回答:1

I am trying to read Parameter values in C# which are being passed to odata request

在下面的请求,我传递$滤波器和期待阅读的过滤器值

网址:http://localhost:57097/Lead?$filter=AssignedToID eq 21987 and IsDeleted eq false

我使用这个代码来读取参数值HttpContext.Current.Request.QueryString.Get("$filter");

并且它返回为“AssignedToID EQ 21987请将isDeleted和EQ假”

但我期待着阅读AssignedToID即21987请将isDeleted和假即的价值

c# model-view-controller parameters odata url-parameters
1个回答
1
投票

第1步:从控制器动作阅读选项

public IQueryable<_MODELNAME_> Get(ODataQueryOptions<_MODELNAME_> Options)
{

......

}

步骤2:选择阅读BinaryOperatorNode

var binaryOperator = Options.Filter.FilterClause.Expression as BinaryOperatorNode;
string filters = getWhereClause(binaryOperator);

第3步:创建以下递归函数来找到所有过滤器值

private static string getWhereClause(BinaryOperatorNode node)
{  
    var s = "";
    if (node.Left is SingleValuePropertyAccessNode && node.Right is ConstantNode)
    {
        var property = node.Left as SingleValuePropertyAccessNode ?? node.Right as SingleValuePropertyAccessNode;
        var constant = node.Left as ConstantNode ?? node.Right as ConstantNode;

        if (property != null && property.Property != null && constant != null && constant.Value != null)
        {
            s += $" {property.Property.Name} {getStringValue(node.OperatorKind)} '{constant.Value}' ";
        }
    }
    else
    {
        if (node.Left is BinaryOperatorNode)
            s += getWhereClause(node.Left as BinaryOperatorNode);

        if (node.Right is BinaryOperatorNode)
        {
            s += $" {getStringValue(node.OperatorKind)} ";
            s += getWhereClause(node.Right as BinaryOperatorNode);
        }
    }
    return s;
}
© www.soinside.com 2019 - 2024. All rights reserved.