Tim*_*Tim 10 xml content-type asp.net-web-api asp.net-core
如何让我的vnext API返回XML和JSON?
我认为使用带有application/xml的content-type可以像以前一样工作.请注意,我尝试使用Accept:application/xml.
但似乎没有.
编辑:
这是我的project.json文件:
{
"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"
]
}
Run Code Online (Sandbox Code Playgroud)
这是我的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();
}
}
Run Code Online (Sandbox Code Playgroud)
hso*_*sop 12
发布.Net Core 1.0.0后更新了详细信息
startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc(config =>
{
// Add XML Content Negotiation
config.RespectBrowserAcceptHeader = true;
config.InputFormatters.Add(new XmlSerializerInputFormatter());
config.OutputFormatters.Add(new XmlSerializerOutputFormatter());
});
Run Code Online (Sandbox Code Playgroud)
project.json
"dependencies": {
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Mvc.Formatters.Xml": "1.0.0",
Run Code Online (Sandbox Code Playgroud)
有关更多帮助,请参阅Shawn Wildermuths关于此主题的博客文章:ASP.NET Core中的内容协商
Kir*_*lla 10
默认情况下,Xml格式化程序不包含在Microsoft.AspNet.Mvc
程序包中.您需要引用另一个Microsoft.AspNet.Mvc.Xml
为此调用的包.
有关如何添加格式化程序的示例:
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());
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10277 次 |
最近记录: |