1
我正在構建一個Web API,可以在啓動類中添加輸入/輸出格式化程序。這適用於XML,但不適用於Json。我知道Json是默認添加的,但是如果Accept頭沒有被指定,它似乎選擇XML。.Net Core 1.1 Web API Json輸入/輸出格式化程序
public void ConfigureServices(IServiceCollection services)
{
//Add framework services.
services.AddMvc(options =>
{
options.RequireHttpsPermanent = true;
options.RespectBrowserAcceptHeader = true;
options.ReturnHttpNotAcceptable = true;
options.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
options.InputFormatters.Add(new XmlDataContractSerializerInputFormatter());
});
services.AddDbContext<CustomerManagerContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))
);
services.AddScoped<IUnitOfWork, UnitOfWork>();
}
在配置服務方法我增加了的XmlSerializer然而,這並不對JSON的工作:
options.OutputFormatters.Add(new JsonOutputFormatter());
默認格式是第一個加入到格式化的列表。我想在XML之前添加Json格式化程序,以便它成爲默認格式化程序。我錯過了什麼?如何正確地添加Json格式器,使其首先位於格式化器列表中?