【问题标题】:How to work with OData primitives or complex types如何使用 OData 基元或复杂类型
【发布时间】:2014-08-22 13:56:14
【问题描述】:

我是第一次使用 OData,并且有一个具有以下属性的模型实体:

public IEnumerable<string> Genres { get; set; }
public IEnumerable<string> GenresFiltered { get; set; }

当我通过网络调用从模型中检索数据时,我收到以下消息:

'类型'xxxx'的属性'Genres'不是有效的属性。不支持类型为基元集合或复杂类型的属性

有没有办法解决此错误以在 Odata 中显示字符串列表?

【问题讨论】:

标签: c# odata


【解决方案1】:

很奇怪。我写了一个小测试 web api odata 服务,它可以工作。代码如下。

WebApiConfig.cs

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
        builder.EntitySet<Foo>("Foos");
        config.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
    }
}

模型类

public class Foo
{
    public string ID { get; set; }
    public IEnumerable<string> Genres { get; set; }
}

控制器类

public class FoosController : ODataController
{
    // GET odata/Foos
    [EnableQuery]
    public IHttpActionResult Get()
    {
        return Ok(FakeData.GetFoos().AsQueryable());
    }
}

然后我尝试了http://localhost:37312/odata/Foos,结果是

{
"@odata.context": "http://localhost:37312/odata/$metadata#Foos",
"value": [
    {
        "ID": "1",
        "Genres": [
            "aaa",
            "bbb"
        ]
    },
    {
        "ID": "2",
        "Genres": [
            "ccc"
        ]
    }
]

}

希望这能有所帮助。

【讨论】:

    猜你喜欢
    • 2012-10-05
    • 2013-12-03
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多