【问题标题】:.NET Web API - Unexpected type.NET Web API - 意外类型
【发布时间】:2012-03-29 12:23:43
【问题描述】:

使用新的 ASP.NET MVC Web API,我收到以下错误

The type InfoboardPage was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.

我正在尝试返回所有派生自同一类的对象列表:InfoboardItem

我已按照说明为每个涉及的类型添加了XmlInclude 属性,但我仍然收到此错误。

[HttpGet]
[XmlInclude(typeof(InfoBoardEvent))]
[XmlInclude(typeof(InfoboardPage))]
[XmlInclude(typeof(InfoboardEventVisitor))]
public List<InfoboardItem> Pages(int ClientNum, int SiteNum, int LangNum)
{
    Data infoboardData = new Data(ClientNum, SiteNum, LangNum, 1);
    var obRetVal = infoboardData.GetPageSequence("~/InfoBoard");

    return obRetVal;
}

以前有人见过这个问题吗?有解决办法吗?还是产品有问题?

【问题讨论】:

    标签: c# serialization asp.net-web-api


    【解决方案1】:

    您需要将 [XmlInclude] 声明添加到 InfoboardItem 类型,而不是操作中,以将派生元素作为 XML 返回。

    【讨论】:

    • 这似乎成功了,非常感谢。现在解决新的模糊错误!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    相关资源
    最近更新 更多