【发布时间】:2016-11-26 16:29:31
【问题描述】:
使用 Web API 和 .NET Core,当我返回 XML 结果时,我得到以下信息:
<Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Response>
<Status>0</Status>
</Response>
</Test>
如何删除命名空间?
这是我的 startup.cs 文件的一部分
services
.AddMvc(config =>
{
// Add XML Content Negotiation
config.RespectBrowserAcceptHeader = true;
config.InputFormatters.Add(new XmlSerializerInputFormatter());
config.OutputFormatters.Add(new XmlSerializerOutputFormatter(new XmlWriterSettings
{
OmitXmlDeclaration = true,
}));
})
.AddXmlDataContractSerializerFormatters()
.AddXmlSerializerFormatters()
.AddMvcOptions(opt => opt.FormatterMappings.SetMediaTypeMappingForFormat("xml", new MediaTypeHeaderValue("application/xml")));
【问题讨论】:
-
你用 RTM 试过了吗?在 RTM 已经推出一段时间后仍然使用 RC1/RC2 听起来不是一个好计划。更好的迁移,可能在 RTM 中已经修复了
-
我应该提到我正在使用 RTM。
-
我的错误。我将它与从 RTM 中删除的一些东西混淆了。您是否删除了以前的 OutputFormatters?即在添加之前调用
.OutputFormatters.Clear()?我假设.AddXmlSerializerFormatters()已经添加了格式化程序,所以您的选项可能没有生效 -
至少
AddXmlSerializerFormatters()调用github.com/aspnet/Mvc/blob/1.0.0/src/…MvcXmlSerializerMvcOptionsSetup添加github.com/aspnet/Mvc/blob/1.0.0/src/… -
另外值得尝试
var outputFormatter = options.OutputFormatters.OfType<XmlSerializerOutputFormatter>().Single()获取XmlSerializerOutputFormatter类型的第一项,然后设置它的属性outputFormatter.WriterSettings.OmitXmlDeclaration = true
标签: asp.net-web-api asp.net-core .net-core