C#中的WebApi响应Accept头,自动返回xml或者json

Global.asax.cs中的Application_Start方法添加

GlobalConfiguration.Configuration.Formatters.Clear(); GlobalConfiguration.Configuration.Formatters.Add(new XmlMediaTypeFormatter()); GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter());

默认就有4个Formatter。不需要添加

.net core写在public void ConfigureServices(IServiceCollection services) 方法中

builder.Services.AddControllers(op => { op.RespectBrowserAcceptHeader = true; }).AddXmlSerializerFormatters();

postman参考:

如果使用Dynamic类型,需要添加xmlInClude来指定序列化的类

cs 复制代码
 await Task.Run(new Action(() =>
 {
     response.data = _db.Database.SqlQueryRaw<OutputEntityM>(sql).FirstOrDefault();

 }));
cs 复制代码
   [XmlInclude(typeof(OutputEntityC))]
   [XmlInclude(typeof(OutputEntityM))]
   public class ResponseEntity
   {
       public string flag { get; set; }
       public string message { get; set; }
      
       public dynamic data { get; set; }

   }
相关推荐
初见_Dream9 小时前
Retrofit+OkHttp+ViewModel
xml·okhttp·retrofit
wolf犭良10 小时前
14、《SpringBoot+MyBatis集成(2)——进阶配置XML与注解的灵活运用》
xml·spring boot·mybatis
Lojarro19 小时前
JavaEE基础之- xml
xml·数据库·java-ee
m0_748233881 天前
使用 Logback 的最佳实践:`logback.xml` 与 `logback-spring.xml` 的区别与用法
xml·spring·logback
zhencon-com2 天前
如何使用qt开发一个xml发票浏览器,实现按发票样式显示
xml·c++·经验分享·python·qt·学习·金融
火烧屁屁啦2 天前
【JavaEE进阶】MyBatis通过XML实现增删改查
xml·java-ee·mybatis
m0_748252234 天前
【mybatis】基本操作:详解Spring通过注解和XML的方式来操作mybatis
xml·spring·mybatis
veminhe6 天前
maven使用默认settings.xml配置时,Idea基于pom.xml更新依赖时报错,有些组件下载时连接超时
xml·maven·intellij-idea
宇内流云9 天前
Tinyfox 简易教程之:Hello World!
websocket·webapi·tinyfox
让代码飞~10 天前
java.lang.NoClassDefFoundError: javax/xml/bind/ValidationException
xml·java·开发语言