ASP.NET 1.0的核心API网页不返回XML [英] ASP.NET Core 1.0 Web API doesn't return XML
问题描述
如何能有我vnext API返回XML和JSON?
我想到用内容类型与应用程序/ XML会的工作,因为它以前。
请注意,我tryed与接受:应用/ XML太
但似乎并非如此。
编辑:
这是我的project.json文件:
{
根目录:wwwroot的
版本:1.0.0- *, 依赖:{
Microsoft.AspNet.Server.IIS:1.0.0-BETA4
Microsoft.AspNet.Server.WebListener:1.0.0-BETA4
Microsoft.AspNet.Mvc:6.0.0-BETA4,
Microsoft.AspNet.Mvc.Xml:6.0.0-BETA4
}, 命令:{
网络:Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls的http://本地主机:5000
}, 构架: {
dnx451:{},
dnxcore50:{}
}, publishExclude:[
node_modules
bower_components
**。xproj
**。用户,
**。vspscc
]
排除:[
wwwroot的
node_modules
bower_components
]
}
这是我的startup.cs:
公共类启动
{
//有关如何配置应用程序的更多信息,请访问:http://go.microsoft.com/fwlink/?LinkID=398940
公共无效ConfigureServices(IServiceCollection服务)
{
services.ConfigureMvc(选项=>
{
//options.AddXmlDataContractSerializerFormatter(); options.InputFormatters.Add(新XmlSerializerInputFormatter());
options.OutputFormatters.Add(新XmlSerializerOutputFormatter());
});
services.AddMvc();
} 公共无效配置(IApplicationBuilder应用程序)
{
app.UseMvc();
}
}
这是默认XML格式化不包括作为 Microsoft.AspNet.Mvc
包的一部分。您需要引用另一个包名为 Microsoft.AspNet.Mvc.Xml
这一点。
你如何可以添加格式化示例:
公开的IServiceProvider ConfigureServices(IServiceCollection服务)
{
services.AddMvc(); services.ConfigureMvc(选项=>
{
//此基础上增加DataContractSerializer的输入和输出格式化
options.AddXmlDataContractSerializerFormatter(); //要添加的XmlSerializer基于输入和输出格式化。
options.InputFormatters.Add(新XmlSerializerInputFormatter());
options.OutputFormatters.Add(新XmlSerializerOutputFormatter());
});
How can I have my vnext API to return XML and JSON ?
I thought that using content-type with application/xml would work as it was before. Note that I tryed with Accept : application/xml too.
But it seems not.
EDIT :
this is my project.json file :
{
"webroot": "wwwroot",
"version": "1.0.0-*",
"dependencies": {
"Microsoft.AspNet.Server.IIS": "1.0.0-beta4",
"Microsoft.AspNet.Server.WebListener": "1.0.0-beta4",
"Microsoft.AspNet.Mvc": "6.0.0-beta4",
"Microsoft.AspNet.Mvc.Xml": "6.0.0-beta4"
},
"commands": {
"web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000"
},
"frameworks": {
"dnx451": { },
"dnxcore50": { }
},
"publishExclude": [
"node_modules",
"bower_components",
"**.xproj",
"**.user",
"**.vspscc"
],
"exclude": [
"wwwroot",
"node_modules",
"bower_components"
]
}
this is my startup.cs :
public class Startup
{
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.ConfigureMvc(options =>
{
//options.AddXmlDataContractSerializerFormatter();
options.InputFormatters.Add(new XmlSerializerInputFormatter());
options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
});
services.AddMvc();
}
public void Configure(IApplicationBuilder app)
{
app.UseMvc();
}
}
By default Xml formatters are not included as part of the Microsoft.AspNet.Mvc
package. You need to reference another package called Microsoft.AspNet.Mvc.Xml
for this.
Example on how you can add the formatters:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.ConfigureMvc(options =>
{
// This adds both Input and Output formatters based on DataContractSerializer
options.AddXmlDataContractSerializerFormatter();
// To add XmlSerializer based Input and Output formatters.
options.InputFormatters.Add(new XmlSerializerInputFormatter());
options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
});
这篇关于ASP.NET 1.0的核心API网页不返回XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!