目前我正在使用这个 JsonFormatter 将驼峰式数据发送到客户端
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver();
WebApiConfig
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Web.Http;
namespace AlumCloud
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
((Newtonsoft.Json.Serialization.DefaultContractResolver)config.Formatters.JsonFormatter.SerializerSettings.ContractResolver).IgnoreSerializableAttribute = true;
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver();
}
}
}
一切工作完美,我的数据按预期完美地在客户端接收。
现在,是时候开始向服务器发送数据了,我正在使用
C# Web Api in the .NET platform.
这是我尝试使用一个 C# 对象将骆驼大小写 JSON 数据发布到的 POST 函数,为了清楚起见,我删除了大部分代码。
WebApi 控制器
namespace AlumCloud.Controllers
{
[Authorize]
public class HorizontalController : AlumCloudWebApiBaseController
{
public async Task<HttpResponseMessage> Post(Horizontal h)
{
string update = HttpContext.Current.Request.QueryString["a"];
int ownerID = 0; ;
HttpResponseMessage res = null;
InsertHorizontalHelper insertRes = null;
if (!int.TryParse(HttpContext.Current.Request.QueryString["id"], out ownerID))
{
return Request.CreateResponse(HttpStatusCode.NotFound, "Provide the id of the horizontal owner");
};
if (h.CompID < 1)
{
if (h.Sitelines.TopCompID > 0)
{
h.CompID = h.Sitelines.TopCompID;
};
}
switch (update)
{
这是水平对象的属性的样子,它们是 Pascal Cased
C# 客户对象我想将属性从 JSON 映射到 C#
namespace CADDL.DataTransfer.Tables
{
[XmlType(Namespace = "urn:DataObjects")]
[XmlRoot(Namespace = "urn:DataObjects")]
[Serializable]
public class Horizontal : ColumnInfo, IHorizontal
{
[ColumnAttributes("ID", false, "int")]
public int ID { get; set; }
[ColumnAttributes("Position", false, "byte")]
public byte Position { get; set; }
[ColumnAttributes("Name", false, "string")]
public string Name { get; set; }
[ColumnAttributes("IsFiller", false, "bool")]
public bool IsFiller { get; set; }
[ColumnAttributes("HorizontalNote", false, "string")]
public string HorizontalNote { get; set; }
[ColumnAttributes("SizeID", false, "int")]
public int SizeID { get; set; }
[ColumnAttributes("WidthInches", false, "decimal")]
public decimal WidthInches { get; set; }
[ColumnAttributes("HeightInches", false, "decimal")]
我的错误
{"message":"An error has occurred.","exceptionMessage":"Object reference not set to an instance of an object.","exceptionType":"System.NullReferenceException","stackTrace":" at AlumCloud.Controllers.HorizontalController.<Post>d__0.MoveNext() in D:\\Users\\Erik Little\\Documents\\visual studio 2015\\Projects\\AlumCloud\\AlumCloud\\Controllers\\CAD\\HorizontalController.cs:line 28\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Threading.Tasks.TaskHelpersExtensions.<CastToObject>d__3`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"}
我对小组的问题是。
- 为什么我的 Horizontal C# 对象总是 null 是因为 JSON 是 Camel 大小写,而它匹配的属性是 Pascal Cased?
- 从 JSON 到 C# 对象的属性映射器是否必须是相同的大小写才能设置值?
- 解决此问题的全局方法是什么,以便我不必在每个新的 Web Api 控制器中添加属性设置?
只是为了清楚起见,您在每个属性上看到的属性是针对我的数据层的,因为在请求数据时,它们不会在此实例中使用