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

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

    This is simplified in RC2 to just services.AddMvc().AddXmlDataContractSerializerFormatters();

    public void ConfigureServices(IServiceCollection services)
            {
                // Add framework services.
                services.AddDbContext<ApplicationDbContext>(options =>
                    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    
                services.AddIdentity<ApplicationUser, IdentityRole>()
                    .AddEntityFrameworkStores<ApplicationDbContext>()
                    .AddDefaultTokenProviders();
    
                services.AddMvc();
    
                services.AddMvc().AddXmlDataContractSerializerFormatters();
    
                // Add application services.
                services.AddTransient<IEmailSender, AuthMessageSender>();
                services.AddTransient<ISmsSender, AuthMessageSender>();
            }
    
    0 讨论(0)
  • 2020-12-19 02:41

    MVC 6 RespectBrowserAcceptHeader is false by default. Hence it will bypass the content negotiation. And that's why probably you're getting XML always after enabling the XML formatter.

    You can turn the RespectBrowserAcceptHeader to true by adding the following to your startup file:

    services.Configure<MvcOptions>(options =>
            {
                options.RespectBrowserAcceptHeader = true;
            });
    
    0 讨论(0)
提交回复
热议问题