Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# OData过滤器,带;及;操作员不在Web Api中工作_C#_Azure_Asp.net Web Api2_Odata_Odata V4 - Fatal编程技术网

C# OData过滤器,带;及;操作员不在Web Api中工作

C# OData过滤器,带;及;操作员不在Web Api中工作,c#,azure,asp.net-web-api2,odata,odata-v4,C#,Azure,Asp.net Web Api2,Odata,Odata V4,我正在创建一个Web Api,其中包含处理以下GET请求的单个GET操作 获取?$filter=Id eq'1234'$select=Name(这一个可以正常工作) 获取?$filter=Id eq“1234”或MessageType eq“1”(这可以正常工作) 获取?$filter=Id eq“1234”和MessageType eq“1”(此项无效。响应值始终为[]) 带“and”运算符的筛选器似乎不工作。“或”接线员对我来说很好 我的webapiconfig.cs中有以下代码 publi

我正在创建一个Web Api,其中包含处理以下GET请求的单个GET操作

获取?$filter=Id eq'1234'$select=Name(这一个可以正常工作)

获取?$filter=Id eq“1234”或MessageType eq“1”(这可以正常工作)

获取?$filter=Id eq“1234”和MessageType eq“1”(此项无效。响应值始终为[])

带“and”运算符的筛选器似乎不工作。“或”接线员对我来说很好

我的webapiconfig.cs中有以下代码

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();
        config.MapODataServiceRoute("odata", "v1/RoutePrefix", GetEdmModel(), new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));
        config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);
    }
    private static IEdmModel GetEdmModel()
    {
        ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
        builder.Namespace = "Default";
        builder.ContainerName = "DefaultContainer";
        builder.EntitySet<Model>("Route");
        builder.EntitySet<Model>("Route").EntityType.Filter(nameof(Model.Id));
        builder.EntitySet<Model>("Route").EntityType.Filter(nameof(Model.MessageType));
        var edmModel = builder.GetEdmModel();

        return edmModel;
    }
}
公共静态类WebApiConfig
{
公共静态无效寄存器(HttpConfiguration配置)
{
config.maphttpAttribute路由();
config.MapODataServiceRoute(“odata”、“v1/RoutePrefix”、getedModel()、新的DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));
config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);
}
私有静态IEdmModel GetEdmModel()
{
ODataConventionModelBuilder=新ODataConventionModelBuilder();
builder.Namespace=“Default”;
builder.ContainerName=“DefaultContainer”;
建筑商实体集(“路线”);
builder.EntitySet(“Route”).EntityType.Filter(nameof(Model.Id));
builder.EntitySet(“Route”).EntityType.Filter(nameof(Model.MessageType));
var edmModel=builder.GetEdmModel();
返回模型;
}
}
在我的控制器中,根据过滤器参数的数量,我调用不同的方法。这两个方法都返回List作为GET方法的响应。在主get方法中,我以Ok(List.AsQueryable())返回

我用[EnableQuery]属性修饰了控制器,并实现了ODataController,如下所示:

[启用查询] 公共类路由控制器:ODataController

Get方法如下所示:

public IHttpActionResult Get()
{
        List<Model> response = null;
        string cacheKey = string.Empty;
        var queryString = Request.RequestUri.PathAndQuery.Replace("/v1/RoutePrefix", "");
        var queryTokens = ParseQueryString(EdmModel, queryString);

        if (queryTokens == null || queryTokens.Any(a => string.IsNullOrWhiteSpace(a.Value)) || !queryTokens.ContainsKey(Constants.Id))
        {
            IList<ApiError> errors = new List<ApiError>() { new ApiError(Constants.InvalidQueryStringErrorCode, Constants.InvalidQueryStringErrorMessage) };
            return GenerateResponse(Request, HttpStatusCode.BadRequest, errors, null);
        }
        else
        {
            try
            {
                if (queryTokens.ContainsKey(Constants.MessageType))
                {
                    response = GetConfigurationByMessageTypeAndId(queryTokens);
                }
                else
                {
                    response = GetConfigurationById(queryTokens);
                }
            }
            catch (Exception ex)
            {
                var apiError = Utilities.CreateApiError(Constants.InternalServerError, Constants.InternalServerErrorMessage, null, null, null);
                IList<ApiError> apiErrors = new List<ApiError> { apiError };

                return GenerateResponse(Request, HttpStatusCode.InternalServerError, apiErrors, null);
            }

            if (response.Count > 0)
            {
                return Ok(response.AsQueryable());
            }
            else
            {
                return NotFound();
            }
        }
    }
public IHttpActionResult Get()
{
列表响应=null;
string cacheKey=string.Empty;
var queryString=Request.RequestUri.PathAndQuery.Replace(“/v1/RoutePrefix”,”);
var queryTokens=ParseQueryString(EdmModel,queryString);
if(queryTokens==null | | queryTokens.Any(a=>string.IsNullOrWhiteSpace(a.Value))| |!queryTokens.ContainsKey(Constants.Id))
{
IList errors=new List(){new apierro(Constants.InvalidQueryStringErrorCode,Constants.InvalidQueryStringErrorMessage)};
返回GenerateResponse(请求,HttpStatusCode.BadRequest,错误,null);
}
其他的
{
尝试
{
if(queryTokens.ContainsKey(Constants.MessageType))
{
响应=GetConfigurationByMessageTypeAndId(queryTokens);
}
其他的
{
响应=GetConfigurationById(queryTokens);
}
}
捕获(例外情况除外)
{
var apierro=Utilities.CreateApiError(Constants.InternalServerError,Constants.InternalServerErrorMessage,null,null);
IList APIRERRORS=新列表{APIRROR};
返回GenerateResponse(请求,HttpStatusCode.InternalServerError,APIRERRORS,null);
}
如果(response.Count>0)
{
返回Ok(response.AsQueryable());
}
其他的
{
返回NotFound();
}
}
}
请让我知道我做错了什么


感谢

问题已解决。$filter系统查询选项允许客户端筛选由请求URL寻址的资源集合。为集合中的每个资源计算用$filter指定的表达式,并且响应中仅包括表达式计算结果为true的项。表达式计算结果为false或null的资源,或由于权限而不可用的引用属性,将从响应中忽略。我的最终集合没有在查询字符串中传递的原始值。它被替换为不同的值–Kumaraguru 1分钟前编辑

MessageType
是否为
enum
<代码>整数<代码>字符串?因为如果我没有错的话,你是在用数字作为字符串。尝试使用
https://localhost:44378/v1/RoutePrefix/Route?$filter=Id eq 1234和MessageType eq 1
MessageType为字符串。如果我使用“或”运算符,则此操作有效。如果我使用“and”运算符,则不会产生任何结果。问题已解决。$filter系统查询选项允许客户端筛选由请求URL寻址的资源集合。为集合中的每个资源计算用$filter指定的表达式,并且响应中仅包括表达式计算结果为true的项。表达式计算结果为false或null的资源,或由于权限而不可用的引用属性,将从响应中忽略。我的最终集合没有在查询字符串中传递的原始值。它被替换为不同的值