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

   }
相关推荐
hasty4 天前
policy.xml 在,不代表策略生效:ImageMagick 补丁给安全配置验收的提醒
xml·mysql·安全
泡海椒8 天前
JQuick-Excel JQuickRow 数据准备实战:JObjectConverter、JQuickRow.toRows 与 XML 导出链路
xml·windows·excel
泡海椒10 天前
JQuick-Excel 字段映射实战:用 MAPPING 固化 Excel 表头与业务字段契约
xml·java·开发语言·excel
csdn_aspnet20 天前
.NetCore Webapi 接口验证请求来源
.netcore·ip·token·签名·webapi·cors·sign
执明wa22 天前
Android RecyclerView 多类型, 多种 Item
android·xml·开发语言·设计模式·android studio
wl851123 天前
SAP CPI 教程004 如何通过groovy修改XML节点属性值
xml
码匠许师傅24 天前
【C++三方组件】TinyXML2:两个文件的极简 XML 解析器
xml·c++
yuzhiboyouye25 天前
my-batisPlus什么时候用xml什么时候用mapper自带的方法,这两种写法专业一点叫什么,什么场景分别使用
xml
筝筝ba1 个月前
如何跳过FFDC
xml·linux·运维·服务器·网络
yuzhiboyouye1 个月前
那xml对应的sql语法,列举一下
xml·数据库·sql