3

如果您使用 WCF,有很多不同的方法可以替换 DataContractResolver,我想用 Web Api 做同样的事情。我发现的唯一扩展点是:

GlobalConfiguration.Configuration.Formatters.XmlFormatter.SetSerializer<Person>(new DataContractSerializer(typeof(Person), null, Int32.MaxValue, false, false, null, new TypeNameVersioning()));

我正在寻找(伪代码)行中的东西:

GlobalConfiguration.Configuration.Formatters.XmlFormatter.SetDataContractResolver = new TypeNameVersioning();
4

1 回答 1

0

如果你想使用 XmlSerializer 那么

GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true

如果那不能满足您的需求,请执行

GlobalConfinguration.Configuration.Formatters.Clear();
GlobalConfinguration.Configuration.Formatters.Add(new YourOwnFormatterCanGoHere());

只是另一点。我一直使用 Web API 并且已经使用了好几年,而且我很少使用格式化程序。我用 HttpContent 的派生版本返回 HttpResponseMessage。不是说你应该做同样的事情,只是说在 Web API 中有很多做事的方法。不需要格式化程序。

于 2014-01-15T14:06:17.390 回答