【发布时间】:2017-04-02 22:18:45
【问题描述】:
我在我的应用程序使用网络API使用剑道调度,以拉动数据从我的数据库。我创建了一个Web API函数,只是硬编码一些数据在那里,以确保剑道计划能读懂我的数据。这是我的API函数的代码:
[Route("api/v1/Events/GetPersonalEvents", Name = "ApiEventsGetPersonalEvents")]
[HttpGet]
public DataSourceResult GetPersonalEvents([System.Web.Http.ModelBinding.ModelBinder(typeof(WebApiDataSourceRequestModelBinder))]DataSourceRequest request)
{
var q = new ViewModels.Events.EventViewModel();
q.Id = 1;
q.Title = "This is a test";
q.Start = DateTime.Now;
q.End = DateTime.Now.AddHours(1);
q.Description = "Test entry";
var list = new List<ViewModels.Events.EventViewModel>();
list.Add(q);
return list.ToDataSourceResult(request);
}
在剑道调度程序没有被示出在日历上任何东西。使用招,我能看到剑道计划在呼唤着我的API和我的API被返回的数据。这里是JSON获得发送:
{
"data":[
{
"id":1,
"title":"This is a test",
"description":"Test entry",
"isAllDay":false,
"start":"2016-11-18T15:31:33.1173519-08:00",
"end":"2016-11-18T16:31:33.1178524-08:00",
"startTimezone":null,
"endTimezone":null,
"recurrenceRule":null,
"recurrenceException":null
}
],
"total":1,
"aggregateResults":null,
"errors":null
}
一切似乎是工作的罚款。经进一步调查,我终于想通了,我的问题。在我的global.asax.cs文件我有这行: P>
HttpConfiguration config = GlobalConfiguration.Configuration;
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.JsonFormatter.UseDataContractJsonSerializer = false;
这样做是它会导致JSON.Net自动C#名称转换为Javascript友好的名称(如Title变得title,Description变得description等),这是我想。然而,剑道,很显然,需要的名称为如C#(例如Title代替title的)。我证实了这一在我的global.asax.cs文件注释掉那些三线和一切工作正常。 P>
所以,后来我把我的关注我的视图模型。我饰我的属性与JsonProperty属性,指定一个特定的名称。然而,它仍然被序列化为小写的名字。下面是视图模型代码:
public class EventViewModel : ISchedulerEvent
{
[JsonProperty(PropertyName = "Id")]
public int Id { get; set; }
[JsonProperty(PropertyName = "Title")]
public string Title { get; set; }
[JsonProperty(PropertyName = "Description")]
public string Description { get; set; }
[JsonProperty(PropertyName = "IsAllDay")]
public bool IsAllDay { get; set; }
[JsonProperty(PropertyName = "Start")]
public DateTime Start { get; set; }
[JsonProperty(PropertyName = "End")]
public DateTime End { get; set; }
[JsonProperty(PropertyName = "StartTimezone")]
public string StartTimezone { get; set; }
[JsonProperty(PropertyName = "EndTimezone")]
public string EndTimezone { get; set; }
[JsonProperty(PropertyName = "RecurrenceRule")]
public string RecurrenceRule { get; set; }
[JsonProperty(PropertyName = "RecurrenceException")]
public string RecurrenceException { get; set; }
}
所以,现在我的想法说出来。那么,有没有办法要么办法让Json.Net连载我的名字正确只为这一个方法或者是有一些其他的属性,我可以在我的视图模型使用,使名称正确序列化或者是有剑道,一个设置将允许剑道使用骆驼情况下格式?
【问题讨论】:
标签: c# asp.net-mvc kendo-ui json.net kendo-scheduler