在ASP.NET API2中强行输出XML

来源:互联网 发布:网络电信诈骗防范措施 编辑:程序博客网 时间:2024/06/05 20:45
在ASP.NET API2中强行输出XML
public async Task<IHttpActionResult> Post(string id, Terminal term) {<span style="white-space:pre"></span>if (!ModelState.IsValid) return BadRequest(ModelState);<span style="white-space:pre"></span>XElement xml = XElement.Parse(@"<?xml version=""1.0"" encoding=""utf-8""?><data><code>1</code><msg>success</msg></data>");<span style="white-space:pre"></span>xml.Element("code").Value = term.code.ToString();        xml.Element("msg").Value = term.msg;<span style="white-space:pre"></span>xml.Add(new XElement("str", "123"));<span style="white-space:pre"></span>var result = new HttpResponseMessage { Content = new StringContent(@"<?xml version=""1.0"" encoding=""utf-8""?>" + xml.ToString(SaveOptions.DisableFormatting), System.Text.Encoding.GetEncoding("UTF-8"), "application/xml") };        return ResponseMessage(result);}

关于MIME类型

text/xml忽略xml头所指定编码格式而默认采用us-ascii编码
application/xml会根据xml头指定的编码格式来编码


MIME类型列表

http://www.w3school.com.cn/media/media_mimeref.asp

                                             
0 0
原创粉丝点击