JSON序列化
当您在代码中使用Grid或其他数据绑定小部件时,请确保在序列化期间属性名大小写不更改。
数据绑定的Telerik UI组件(如Grid)依赖于来自服务器的Pascal大小写格式响应。但是,在ASP中JSON字符串的默认大小写。NET Core是Camel案例。如果序列化器将大小写更改为Camel,则数据绑定小部件不能正确显示数据。
本文档描述了在不同的ASP中维护Pascal用例的推荐方法。NET核心版本。
配置JSON序列化在ASP。网络核心6 and the Minimal Hosting Model
对于使用。net 6和最小托管模型打开Program.cs
文件。要为应用程序设置序列化选项,可以使用下面演示的任何一种方法。
使用与ASP一起交付的默认序列化。NET Core(推荐的方法)。
var构建器= WebApplication.CreateBuilder(args);//向容器中添万博体育手机版网址加服务。//在序列化过程万博体育手机版网址中维护属性名。参见:// https://github.com/aspnet/Announcements/issues/194 .AddJsonOptions(options => options. jsonserializeroptions . propertynamingpolicy = null);//添加Kendo UI服务到服万博体育手机版网址务容器构建器。services . addkendo ();
使用
Newtonsoft
图书馆。var构建器= WebApplication.CreateBuilder(args);//向容器中添万博体育手机版网址加服务。//在序列化过程万博体育手机版网址中维护属性名。参见:// https://github.com/aspnet/Announcements/issues/194 .AddNewtonsoftJson(options => options. serializersettings . contractresolver = new newtonsoft . json . serializer . defaultcontractresolver ());//添加Kendo UI服务到服万博体育手机版网址务容器构建器。services . addkendo ();
配置JSON序列化在ASP。NET Core 3通过ASP。NET Core 5
在ASP中有三种配置JSON序列化的方法。NET Core 3至5:
推荐的方法是使用ASP提供的默认序列化。净的核心。要配置它,请找到
Configure万博体育手机版网址Services
方法,并通过添加下面的代码来更新它。配置服务(IServiceCollecti万博体育手机版网址on services){…万博体育手机版网址services .AddControllersWithViews() .AddJsonOptions(options => options. jsonserializeroptions . propertynamingpolicy = null);//添加Kendo UI服务到服务容器。万博体育手机版网址万博体育手机版网址services.AddKendo ();}
第一种可选方法是全局维护属性名大小写。定位
Configure万博体育手机版网址Services
方法,并通过添加使用Newtonsoft.Json.Serialization;
线在顶部。...使用Newtonsoft.Json.Serialization;...配置服务(IServiceCollecti万博体育手机版网址on services){…万博体育手机版网址services.AddMvc() .AddNewtonsoftJson(options => options. serializersettings . contractresolver = new DefaultContractResolver());//添加Kendo UI服务到服务容器。万博体育手机版网址万博体育手机版网址services.AddKendo ();}
第二种可选方法是在整个应用程序中使用默认的JSON序列化,并包括内置的
System.Text.Json.JsonSerializerOptions
在控制器动作方法中响应。...使用System.Text.Json;...public ActionResult Orders_Read([DataSourceRequest]DataSourceRequest request) {DataSourceResult result = orders.ToDataSourceResult(request);返回Json(结果,新的JsonSerializerOptions() {propertynamecasesensitive = false});}
配置JSON序列化在ASP。NET Core 2
要维护属性名的大小写,请找到Configure万博体育手机版网址Services
方法,并通过添加使用Newtonsoft.Json.Serialization;
线在顶部。
...使用Newtonsoft.Json.Serialization;...配置服务(IServiceCollecti万博体育手机版网址on services){…//在序列化过程中维护属性名。//更多信息,请访问https://github.com/aspnet/Announcements/issues/194。万博体育手机版网址services .AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1) .AddJsonOptions(options => options. serializersettings . contractresolver = new DefaultContractResolver());//添加Kendo UI服务到服务容器。万博体育手机版网址万博体育手机版网址services.AddKendo ();}