ASP.NET Web API中的XML命名空间
本文关键字:XML 命名空间 中的 API NET Web ASP | 更新日期: 2023-09-27 18:20:05
我目前正在处理一个项目,该项目要求我从其端点输出XML和JSON。我有以下型号:
[DataContract(Namespace="http://www.yale.edu/tp/cas")]
[XmlType("serviceResponse")]
[XmlRoot(Namespace="http://www.yale.edu/tp/cas")]
public class ServiceResponse
{
[XmlElement("authenticationSuccess")]
public AuthenticationSuccess Success { get; set; }
[XmlElement("authenticationFailure")]
public AuthenticationFailure Failure { get; set; }
}
当成功不为空时,输出如下:
<serviceResponse>
<authenticationSuccess />
</serviceResponse>
现在,我可以看到,很明显,我没有为我告诉元素要成为其中一部分的名称空间分配前缀。我的问题是,我找不到使用媒体格式化程序在MVC4中添加命名空间前缀的位置。我的global.asax中有以下内容:
GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;
GlobalConfiguration.Configuration.Formatters.XmlFormatter.RemoveSerializer(typeof(Models.ServiceResponse));
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SetSerializer(typeof(Models.ServiceResponse), new Infrastructure.NamespaceXmlSerializer(typeof(Models.ServiceResponse)));
我制作了一个基于XmlSerializer的自定义序列化程序,试图拦截写入请求并将命名空间列表粘贴在上面。这个方法的问题是,现在我在每个可重写的方法中都有断点,并且在序列化时没有一个断点被触发,这让我相信我的序列化程序没有被使用。
是否有一些内置的方法来实现我想要做的事情,或者我是否在重新实现XmlMediaTypeFormatter以在它序列化对象时传入命名空间?
作为后续答案:对我来说,最简单的解决方案是编写自己的XmlMediaTypeFormatter
。事实证明,它并没有我想象的那么可怕。
public class NamespacedXmlMediaTypeFormatter : XmlMediaTypeFormatter
{
const string xmlType = "application/xml";
const string xmlType2 = "text/xml";
public XmlSerializerNamespaces Namespaces { get; private set; }
Dictionary<Type, XmlSerializer> Serializers { get; set; }
public NamespacedXmlMediaTypeFormatter()
: base()
{
this.Namespaces = new XmlSerializerNamespaces();
this.Serializers = new Dictionary<Type, XmlSerializer>();
}
public override Task WriteToStreamAsync(Type type, object value, System.IO.Stream writeStream, System.Net.Http.HttpContent content, System.Net.TransportContext transportContext)
{
lock (this.Serializers)
{
if (!Serializers.ContainsKey(type))
{
var serializer = new XmlSerializer(type);
//we add a new serializer for this type
this.Serializers.Add(type, serializer);
}
}
return Task.Factory.StartNew(() =>
{
XmlSerializer serializer;
lock (this.Serializers)
{
serializer = Serializers[type];
}
var xmlWriter = new XmlTextWriter(writeStream, Encoding.UTF8);
xmlWriter.Namespaces = true;
serializer.Serialize(xmlWriter, value, this.Namespaces);
});
}
}
以下是格式化程序的要点:https://gist.github.com/kcuzner/eef239003d4f99dfacea
格式化程序通过公开XmlSerializer
将要使用的XmlSerializerNamespaces
来工作。通过这种方式,我可以根据需要添加任意名称空间。
我的顶级型号如下:
[XmlRoot("serviceResponse", Namespace="http://www.yale.edu/tp/cas")]
public class ServiceResponse
{
[XmlElement("authenticationSuccess")]
public CASAuthenticationSuccess Success { get; set; }
[XmlElement("authenticationFailure")]
public CASAuthenticationFailure Failure { get; set; }
}
在我的Global.asax
中,我添加了以下内容,将格式化程序放在列表的顶部:
var xmlFormatter = new Infrastructure.NamespacedXmlMediaTypeFormatter();
xmlFormatter.Namespaces.Add("cas", "http://www.yale.edu/tp/cas");
GlobalConfiguration.Configuration.Formatters.Insert(0, xmlFormatter);
在添加了格式化程序并确保我的属性设置正确之后,我的XML就有了正确的名称空间。
在我的情况下,我需要添加链接到http://www.yale.edu/tp/cas
的cas
命名空间。对于其他使用此功能的人,只需更改/复制Add
调用即可,从而添加命名空间。