ASP.NET网页API返回的XML和XML获得 [英] ASP.NET Web Api to return XML and get XML
本文介绍了ASP.NET网页API返回的XML和XML获得的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个控制器,它以JSON返回数据。我想这个方法返回XML结构和找回数据,以XML结构。
我已经加入以下code到WebApiConfig:
config.Routes.MapHttpRoute(
名称:defaultapi
routeTemplate:API / {}控制器/(编号),
默认:新{ID = RouteParameter.Optional}
);config.Routes.MapHttpRoute(
名称:VehicleApi
routeTemplate:API / {}控制器/(编号),
默认:新{ID = RouteParameter.Optional}
);config.Formatters.XmlFormatter.SupportedMediaTypes.Add(新MediaTypeHeaderValue(文/ XML));
VAR appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(T =&放大器; GT; t.MediaType ==应用程序/ XML);
的Global.asax.cs
GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(新System.Net.Http.Formatting.XmlMediaTypeFormatter());
解决方案
要做出的ASP.NET Web API返回的XML,你不需要做任何code的变化。只要确保你在HTTP请求中像这样的标题。
接受:应用程序/ XML
请参阅这有关内容协商更多信息。
I have a controller which returns a data in json. I would like that method to return XML structure and get data back to XML structure.
I have added following code to WebApiConfig:
config.Routes.MapHttpRoute(
name: "defaultapi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "VehicleApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Formatters.XmlFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/xml"));
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
Global.asax.cs
GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new System.Net.Http.Formatting.XmlMediaTypeFormatter());
解决方案
To make ASP.NET Web API return XML, you don't need to make any code changes. Just ensure you have a header like this in the HTTP request.
Accept: application/xml
See this for more info on content negotiation.
这篇关于ASP.NET网页API返回的XML和XML获得的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文