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

   }
相关推荐
编程、小哥哥7 小时前
手写mybatis之Mapper XML的解析和注册使用
xml·java·mybatis
极客先躯2 天前
Hadoop-HDFS-core-site.xml,hdfs-site.xml,yarn-site.xml,krb5.conf 都是做什么的?
xml·运维·hadoop·hdfs
MarcoPage3 天前
第 30 章 XML
xml
OEC小胖胖3 天前
XPath基础知识点讲解——用于在XML中查找信息的语言
xml·java·javascript·xpath
不拱地的猪4 天前
想知道为什么有DICOM格式,YAML格式,XML格式,JSON格式吗?
xml·java·c++·opencv·json
CV-King4 天前
python实用脚本(二):删除xml标签下的指定类别
xml·开发语言·人工智能·python·opencv
emmm4594 天前
XML与JSON的用法与区别
xml·json
孑么5 天前
GDPU Andriod移动应用 Activity周期与启动
android·xml·java·kotlin·android-studio
Jay的小提琴5 天前
第十章 XML
android·xml·java·开发语言·笔记·idea
亚林瓜子6 天前
使用jaxb来生成多层嵌套xml
xml·jaxb