あなたのIMessage
実装でKnownTypeAttribute
を配置する必要があり、次のアクションを呼び出すとき
public interface IMessage
{
string Content { get; }
}
[KnownType(typeof(Message))]
public class Message : IMessage {
public string Content{ get; set; }
}
[KnownType(typeof(Message2))]
public class Message2 : IMessage
{
public string Content { get; set; }
}
だから:
<ArrayOfanyType xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<anyType xmlns:d2p1="http://schemas.datacontract.org/2004/07/MvcApplication3.Controllers" i:type="d2p1:Message">
<d2p1:Content>value1</d2p1:Content>
</anyType>
<anyType xmlns:d2p1="http://schemas.datacontract.org/2004/07/MvcApplication3.Controllers" i:type="d2p1:Message2">
<d2p1:Content>value2</d2p1:Content>
</anyType>
</ArrayOfanyType>
:
public IEnumerable<IMessage> Get()
{
return new IMessage[] { new Message { Content = "value1" },
new Message2 { Content = "value2" } };
}
を結果はこのことだろうしかし、これは一つの "道"しか働かないでしょう。したがって、同じXMLをポストバックすることはできません。以下のアクションが動作するはずのために
:
public string Post(IEnumerable<IMessage> messages)
あなたはDataContractSerializer
を設定し、設定を使用してGlobalConfiguration.Configuration.Formatters
GlobalConfiguration.Configuration
.Formatters
.XmlFormatter.SetSerializer<IEnumerable<IMessage>>(
new DataContractSerializer(typeof(IEnumerable<IMessage>),
new[] { typeof(Message), typeof(Message2) }));
に設定すると、世界的に知られているタイプを登録する必要があります実装タイプにKnownTypeAttribute
は必要ありません。
? Type自体に[KnownType(Type)]を入れるのは私にとっては奇妙に見えますが、試してみます。 –
奇妙ですが、実際にはうまくいっていますが、片側だけです。私はストリームを渡し、メッセージは常にnull、私はこの多分私のポストクエリが正しくない、またはシリアライザは何を読むことができないためにフィドラーを使用している公開空白Post([FromBody] IEnumerableメッセージ)メソッドがあります書きました。 –
同じXMLをポストバックするとうんざりします。私は 'ArrayOfanyType'と' anyType'のために推測します。私は現在、それを手抜きしています。私が何かを見つけたら私はあなたに戻ってきます。 – nemesv