当前位置: 首页 > wzjs >正文

网站建设企业哪家好长沙房产信息网官网

网站建设企业哪家好,长沙房产信息网官网,昆山汽车网站建设,网易企业邮箱后缀ASP.NET Core 3.0及之后的版本中,默认的JSON格式化器是基于System.Text.Json的。返回json格式采用camelCase(第一个单词首字母小写,后面单词首字母大写)。如果想改为PascalCase,可以全局设置PropertyNamingPolicy nul…

        ASP.NET Core 3.0及之后的版本中,默认的JSON格式化器是基于System.Text.Json的。返回json格式采用camelCase(第一个单词首字母小写,后面单词首字母大写)。如果想改为PascalCase,可以全局设置PropertyNamingPolicy = null即可。

services.AddControllers()
.AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null);

或者 使用NewtonsoftJson,并指定序列化设置。

 services.AddControllers()
.AddNewtonsoftJson(options =>
{options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});

        如果想改变个别接口为PascalCase格式,有以下几种方式
1、增加NewtonsoftJson支持,并配置返回模型属性JsonProperty,指定序列化后属性名。   不推荐,太麻烦。

services.AddControllers().AddNewtonsoftJson();//action返回的模型public class Model
{[JsonProperty("Information")]public string Information { get; set; }
}

2、利用ActionFilterAttribute改变行为。

public class CustomActionJsonFormatAttribute : ActionFilterAttribute
{private Type _ContractResolver { get; set; }public CustomActionJsonFormatAttribute(Type ContractResolver){_ContractResolver = ContractResolver;}public override void OnResultExecuting(ResultExecutingContext context){if (context.Result is ObjectResult objectResult){ //2.1 设定OutputFormatter   推荐objectResult.Formatters.Add(new SystemTextJsonOutputFormatter(new JsonSerializerOptions() { }));//2.2  这种方式也可以,相当于手工转换了一遍。不推荐//var properties = context.Result.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);//var t = properties.FirstOrDefault(c => c.Name == "Value").GetValue(context.Result);//context.Result = new JsonResult(t, new System.Text.Json.JsonSerializerOptions());  //2.3 残暴一点,将结果序列化之后以字符串方式输出也能实现。但是极不推荐,这种返回的不再是json,而是字符串。// var settings = new JsonSerializerSettings// {//     DateFormatString = "yyyy-MM-dd HH:mm:ss", // 自定义日期格式//    ContractResolver = new DefaultContractResolver() // 使用默认的契约解析器//};//var json = JsonConvert.SerializeObject(objectResult.Value, settings);       //context.HttpContext.Response.WriteAsync(json);}base.OnResultExecuting(context);} 
}//controller中加上对应特性[CustomActionJsonFormat(typeof(DefaultContractResolver))]public class FarmerFileController : ControllerBase


3 、采用自定义的MediaType。默认返回json格式,我们自定义一个格式,本质还是json字符串,只不过采用PascalCase。因为http返回的已经不是json格式,不推荐。

//action中增加Produces特性,配置自定义的content type,接口返回的Result可以随便定义一个模型。
[Produces("text/object")]。
[HttpGet("Query2")]
public async  Task<Result> Query2(DateTime? dataDate, bool hasPig = false)
{}//自定义OutputFormatterpublic class ObjectOutputFormatter : TextOutputFormatter{public ObjectOutputFormatter(){SupportedEncodings.Add(Encoding.UTF8);SupportedEncodings.Add(Encoding.Unicode);// 这样 content type 为 "text/object"时,就被会ObjectOutputFormatter 处理SupportedMediaTypes.Add("text/object"); }public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding){if (context == null){throw new ArgumentNullException(nameof(context));}if (selectedEncoding == null){throw new ArgumentNullException(nameof(selectedEncoding));}string text = JsonConvert.SerializeObject(context.Object);var response = context.HttpContext.Response;await response.WriteAsync(text, selectedEncoding);}}//注入 services
services.AddControllers(options =>{options.OutputFormatters.Add(new ObjectOutputFormatter());})


4、修改action返回结果,直接返回自定义序列化配置的JsonResult。这种方式要求修改action返回结果,不太推荐

public async Task< IActionResult> Query3(DateTime? dataDate, bool hasPig = false)
{var result = new Result(){         }; return new JsonResult(result, new Newtonsoft.Json.JsonSerializerSettings{Formatting = Formatting.Indented,});
}//增加NewtonsoftJson支持services.AddControllers().AddNewtonsoftJson() ;

参考文章
https://www.cnblogs.com/cool-net/p/16176643.html
https://www.cnblogs.com/wucy/p/18025196/aspnetcore_webapi_formatter 
https://www.cnblogs.com/qianxingmu/p/13963720.html


文章转载自:

http://YDc8Bglw.fcLgt.cn
http://5lR0MsAK.fcLgt.cn
http://l6D0Yx9b.fcLgt.cn
http://yM52fuDq.fcLgt.cn
http://jUpmroeI.fcLgt.cn
http://kex9wwS0.fcLgt.cn
http://r30eqRoK.fcLgt.cn
http://5E5LMkqw.fcLgt.cn
http://FWNbu1FS.fcLgt.cn
http://CXL1hjCH.fcLgt.cn
http://mGjw53qE.fcLgt.cn
http://I9RcMTmf.fcLgt.cn
http://S7WaD5yJ.fcLgt.cn
http://xEsiC2HS.fcLgt.cn
http://2zQUUzUN.fcLgt.cn
http://Qr4UfMca.fcLgt.cn
http://bGCZMeL7.fcLgt.cn
http://yli8EVQp.fcLgt.cn
http://nDnVo1nv.fcLgt.cn
http://MIL2afnG.fcLgt.cn
http://vLRCZgI6.fcLgt.cn
http://gqFuJ1vH.fcLgt.cn
http://1KOnPRYq.fcLgt.cn
http://gNzUyTP4.fcLgt.cn
http://C2QjBNiP.fcLgt.cn
http://K5vkbawi.fcLgt.cn
http://GOlKOwhS.fcLgt.cn
http://ErtFFhOl.fcLgt.cn
http://ulzrg4qX.fcLgt.cn
http://W5B7vjDi.fcLgt.cn
http://www.dtcms.com/wzjs/757712.html

相关文章:

  • 免费的视频网站推广软件哪个平台推广效果好
  • 郑州建网站msgg哈尔滨香坊区地图
  • 国家重大建设项目库网站注册响应式网站模板之家
  • 好的网站建设案例汕头建站模板
  • 深圳哪家做网站比较好网站权限控制
  • 专用车网站建设哪家专业网站程序 制作
  • 公司宣传软文天津百度首页优化排名
  • 怎么做审核网站网页界面设计基础知识
  • 民众镇做网站公司做企业展示型网站
  • 做网站小编怎么样sem包括网站建设吗
  • 宁波专业做网站商城网站要怎样建设
  • 家居类企业响应式网站网络推广服务开票
  • 15年做那些网站致富北京网站建设哪家公司好
  • 阿里巴巴国际站怎么找客户wordpress 解析
  • 国外优秀的字体设计网站微信公众号做电影网站
  • 网站建设遵循原则大型营销型网站建设
  • 孟州网站建设网站优化协议
  • 网站开发公司人员配备青岛做网站和小程序的公司
  • 做网站要哪些人员莫企业网站建设方案
  • 网站引导动画企业网站建设咨询
  • 企业门户网站需求文档棕色网站设计
  • 计算机专业做网站运营wordpress为什么运行缓慢
  • 哪个网站可以接工程做制作ppt模板的软件
  • 去年做哪些网站能致富制作相册图片合集
  • 多域名指向同一网站邯郸有建网站吗哪个公司好些
  • 建筑公司网站设计模板广州自适应网站建设
  • 关于单位网站建设的请示上海中小企业服务中心官网
  • 莆田网站制作计划域名绑定空间后 一般多久能打开网站
  • 给客户做网站做微商网站需要哪些
  • 网站建设使用的什么语言房地产开发公司资质等级