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

前端 未结 8 878
再見小時候
再見小時候 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:40

    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());
        });
    

提交回复
热议问题