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

   }
相关推荐
数据知道15 小时前
XXE 漏洞深度实战:XML 外部实体攻击与 OOB 带外
xml·网络·安全·网络安全
云水一下17 小时前
零基础玩转bWAPP靶场(四十七):XSS - Reflected (AJAX/XML)
xml·web安全·ajax·xss·bwapp
爱敲键盘的猴子3 天前
深入理解 Spring IoC -- 基于 XML 的 Bean 配置详解
xml·java·spring
前网易架构师-高司机3 天前
带标注的羽毛球运动员,裁判,羽毛球识别数据集,识别率84.4%,2879张图,支持yolo,coco json,voc xml,文末有模型训练代码
xml·yolo·json·数据集·羽毛球·裁判
微微1笑抽了筋7 天前
xml中设置透明度
xml
Source.Liu7 天前
【Uppsala】入口模块(lib.rs)
xml·rust
Source.Liu7 天前
【Uppsala】介绍
xml·rust
Wang's Blog8 天前
AI Agent白手起家37: LangChain 输出解析器实战:文本、JSON、XML 与 Pydantic
xml·langchain·json
initialize13068 天前
Oracle数据库 binary XML data类型同步
xml·数据库
御坂嘀喵 白日焰火8 天前
LINQ之路18:LINQ to XML之导航和查询
xml·solr·linq