ASP.NET Core 1.0 Web API doesn't return XML

前端 未结 8 877
再見小時候
再見小時候 2020-12-19 02:12

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 Accep

8条回答
  •  攒了一身酷
    2020-12-19 02:25

    Here is the updated answer for MVC6 rc1

    Startup.cs (using MvcXmlMvcBuilderExtensions)

        public void ConfigureServices(IServiceCollection services)
        {
            var mvcBuilder = services.AddMvc();
            mvcBuilder.AddXmlSerializerFormatters();
            // or mvcBuilder.AddXmlDataContractSerializerFormatters()
    

    project.json

    "dependencies": {
        "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
        "Microsoft.AspNet.Mvc.Formatters.Xml": "6.0.0-rc1-final",
    

    Don't forget as ASP.NET 5 was renamed to ASP.NET Core 1.0 and so Microsoft.AspNet.Mvc became Microsoft.AspNetCore.Mvc i.e.

    "dependencies": {
       "Microsoft.AspNetCore.Mvc" : "1.0.2",
       "Microsoft.AspNetCore.Mvc.Formatters.Xml" : "1.0.2",
    

提交回复
热议问题