如何在OData v4中获取枚举值而不是枚举名?

时间:2023-01-26 14:43:04

I have an enum that looks like this:

我有一个看起来像这样的枚举:

enum Color : byte
{
    Transparent,
    White,
    Black
}

When I request the values from my Entity I get the results as strings, not integers, example:

当我从我的实体请求值时,我得到的结果是字符串,而不是整数,例如:

{
    "Color": "White"
}

Is there any way to make OData return the value instead of the name of the value?

有没有办法让OData返回值而不是值的名称?

I'm using Microsoft.AspNet.OData v6.1.0, Microsoft.OData.Core v7.2.0, and Microsoft.Odata.Edm v7.2.0.

我正在使用Microsoft.AspNet.OData v6.1.0,Microsoft.OData.Core v7.2.0和Microsoft.Odata.Edm v7.2.0。

The target framework is .Net Framework 4.6.1.

目标框架是.Net Framework 4.6.1。

1 个解决方案

#1


0  

You can try to add a new property, like this:

您可以尝试添加新属性,如下所示:

public byte ColorInt { get { return (byte)Color; }}

I had that same issue and the only solution I found after searching a lot was that.

我有同样的问题,经过搜索后找到的唯一解决方案是。

If you don't want the Color property to be returned you can decorate it with JsonIgnore atribute.

如果您不想返回Color属性,可以使用JsonIgnore属性修饰它。

#1


0  

You can try to add a new property, like this:

您可以尝试添加新属性,如下所示:

public byte ColorInt { get { return (byte)Color; }}

I had that same issue and the only solution I found after searching a lot was that.

我有同样的问题,经过搜索后找到的唯一解决方案是。

If you don't want the Color property to be returned you can decorate it with JsonIgnore atribute.

如果您不想返回Color属性,可以使用JsonIgnore属性修饰它。