WebAPI 2.2 不支持 substringof 函数

Posted

技术标签:

【中文标题】WebAPI 2.2 不支持 substringof 函数【英文标题】:WebAPI 2.2 does not support substringof function 【发布时间】:2014-09-19 14:42:35 【问题描述】:

我有一个支持 OData 的 WebAPI 2.2 服务。

我的控制器有一个返回IQuerable<Entity> 的操作,但即使我允许所有功能,我也无法使用$filter=substringof 功能。

[Authorize]
public class MyController : ODataController

    [EnableQuery(AllowedFunctions=AllowedFunctions.All)]
    public IQueryable<Entity> GetEntities()
    
      return GetMyQueryable();
    

当我点击像 http://localhost:49844/Entities/?$filter=substringof('Queen',Name) 这样的 URL 时

我收到一条错误消息,指出不允许使用 substringof。


"error": 
    "code": "",
    "message": "The query specified in the URI is not valid. An unknown function with name 'substringof' was found. This may also be a function import or a key lookup on a navigation property, which is not allowed.",
    "innererror": 
        "message": "An unknown function with name 'substringof' was found. This may also be a function import or a key lookup on a navigation property, which is not allowed.",
        "type": "Microsoft.OData.Core.ODataException",

知道为什么我会看到这个错误吗?

【问题讨论】:

【参考方案1】:

substringof()V3 函数,而contains()V4 函数。

尝试包含:

$filter=contains(Name,'Queen')

【讨论】:

substringof() 是 V3 函数,而 contains 是 V4 函数。如果包含不起作用,您可以分享错误消息吗? 我错说“包含”给出了同样的错误。使用包含时,它说“URI中指定的查询无效。函数'startswith'是不允许的。要允许它,请在EnableQueryAttribute或QueryValidationSettings上设置'AllowedFunctions'属性。”,但我想我正在做这部分.但是,AllowedFunctions 枚举尚未更新以反映 OData V4 函数的变化 @QianLi 感谢您指出 V3/V4 的区别。我没有注意到它。而且由于 AllowedFunctions 枚举列出了 SubstringOf 而没有包含在那里,我认为 SubstringOf 仍然是要走的路 这不是一个直接的答案,但这为我指明了正确的方向,我仍然不知道确切的问题是什么,但我创建了一个新的 WebAPI 和项目,它只支持包含函数.然后我删除了原始 WebAPI 中所有不必要的 NuGet 包,这似乎解决了这个问题。无需使用 AllowedFunctions 枚举,默认启用一切。 这似乎解决了我的问题,我阅读了错误的文档,谷歌将我带到这里,谢谢。

以上是关于WebAPI 2.2 不支持 substringof 函数的主要内容,如果未能解决你的问题,请参考以下文章

Web Api 2.2 OData V4 函数路由

WebApi 2.2 + Chrome = CORS 405 错误

向 ASP.NET WebApi 2.2 添加身份验证

WebAPI 2.2 中没有命名空间的 OData v4 自定义函数

Angular 7 .net 核心 2.2 WebApi Windows 身份验证 CORS

如何在 .NET Core 2.2 WebApi 和 Azure AD 2.0 中配置 Swagger?