小编NGU*_*YET的帖子

如何使用 StringAsEnumResolver 在 ASP.NET Core 5 OData 中按字符串过滤枚举?

使用最新的 Microsoft.AspNetCore.OData 8.0.0-rc3。

services.AddControllers()
    .AddOData(opt => opt.Count().Filter().Expand().Select().OrderBy().SetMaxTop(100)
        .AddModel(string.Empty, GetEdmModel(),
            builder =>
            {
                builder.AddService(Microsoft.OData.ServiceLifetime.Singleton, typeof(ODataUriResolver), sp => new StringAsEnumResolver() { EnableCaseInsensitive = true });
            })
        .Conventions.Add(new OdataConvention()))
Run Code Online (Sandbox Code Playgroud)

使用上面的代码来配置StringAsEnumResolver,我仍然无法使用字符串来过滤枚举。我在互联网上搜索过但无济于事。

我缺少什么?

错误来自$filter=contactType eq 'person'

{
  "error": {
    "code": "",
    "message": "The query specified in the URI is not valid. The string 'person' is not a valid enumeration type constant.",
    "details": [
      
    ],
    "innererror": {
      "message": "The string 'person' is not a valid enumeration type constant.",
      "type": "Microsoft.OData.ODataException",
      "stacktrace": " …
Run Code Online (Sandbox Code Playgroud)

c# asp.net odata .net-core asp.net-core

6
推荐指数
0
解决办法
1090
查看次数

标签 统计

.net-core ×1

asp.net ×1

asp.net-core ×1

c# ×1

odata ×1