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

   }
相关推荐
爱编程的小庄2 小时前
Maven 4.0.0 模式-pom.xml配置详解
xml·java·maven
百锦再5 小时前
Android Studio 中使用 SQLite 数据库开发完整指南(Kotlin版本)
android·xml·学习·sqlite·kotlin·android studio·数据库开发
wkj0011 天前
springboot + mybatis 需要写 .xml吗
xml·spring boot·mybatis
llkk星期五1 天前
Mujoco xml < sensor>
xml·机器人
weixin_456588152 天前
【Maven】特殊pom.xml配置文件 - BOM
xml·java·maven
百锦再2 天前
Kotlin学习基础知识大全(上)
android·xml·学习·微信·kotlin·studio·mobile
Minyy112 天前
SpringBoot程序的创建以及特点,配置文件,LogBack记录日志,配置过滤器、拦截器、全局异常
xml·java·spring boot·后端·spring·mybatis·logback
北极的企鹅883 天前
XML内容解析成实体类
xml·java·开发语言
yuren_xia3 天前
Spring XML 配置
xml·java·spring
BOB-wangbaohai3 天前
Flowable7.x学习笔记(十四)查看部署流程Bpmn2.0-xml
xml·笔记·学习