0

我正在尝试将数据发布到EntitySetController,但控制器接收到的实体始终是null

配置OData是:

public static class ODataApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                config.Routes.MapODataRoute("DefaultOdata" , "odata" , GetImplicitModel());
            }

        public static IEdmModel GetImplicitModel()
        {
            var builder = new ODataConventionModelBuilder();
            builder.EntitySet<ZipCode>("ZipCodes");        

            return builder.GetEdmModel();
        }
    }

我有以下控制器:

public class ZipCodesController : EntitySetController<ZipCode, String>
    {
         public IUnitOfWork Iuow { get; set; }

         public ZipCodesController(IUnitOfWork unitOfWork)
         {
             Iuow = unitOfWork;
         }

         [Queryable]
         public override IQueryable<ZipCode> Get()
         {
             return Iuow.GetStandardRepository<ZipCode>().GetAll();
         }

         protected override string GetKey(ZipCode entity)
         {
             return entity.Id.ToString();
         }
         protected override ZipCode GetEntityByKey(string key)
         {
             return Iuow.GetStandardRepository<ZipCode>().GetById(key);
         }

         protected override ZipCode CreateEntity(ZipCode entity)
         {
             Iuow.GetStandardRepository<ZipCode>().Add(entity);
             Iuow.Commit();
             return entity;
         }
    }

ZipCode 定义如下:

 public class ZipCode
        {
            public Guid Id { get; set; }
            public long Version { get; set; }
            public String CreatedBy { get; set; }
            public String LastModifiedBy { get; set; }
            public String ZipPostalCode{ get; set; }
            public ZipCodeType ZipCodeType { get; set; }
            public Guid CountryId { get; set; }
            public Guid CountyId { get; set; }
            public Guid StateId { get; set; }
            public Guid CityId { get; set; }
            public String Latitude { get; set; }
            public String Longitude { get; set; }

        }

我正在尝试通过 发布以下实体Fiddler,但收到的实体始终是null

{"Id":"6b146d72-2681-4d47-8cc4-75e64e2dea66", "Version":20, "CreatedBy":"ae5882fb-b833-46d7-9f58-0505ec2a6f8f","LastModifiedBy":"ae5882fb-b833-46d7-9f58-0505ec2a6f8f","ZipPostalCode":"92020","ZipCodeType": 3,"CountryId":"6a54b9be-8726-4376-a99e-989884e2b724","CountyId":"9c4052fa-49f2-4e5e-995f-317abd16814b","StateId":"0da54905-6acd-4886-a9d1-f3e6d9eb7c60","CityId":"ba543bc1-7eb7-4e14-bbea-2ffcac5b2e2c","Latitude":"36.978256","Longitude":"-121.952464"}

我注意到,如果我将versionandZipCodeType字段(所有非引号字段)的引号用作"Version":"20"and "ZipCodeType":"3",一切都很好,收到的实体是not null.

请帮我解决这个问题。

4

1 回答 1

1

OData V3 本身不支持枚举。因此,Web API OData 将它们建模为字符串。这就是为什么你需要遵循字符串语法,即引用。

此外,如果传入参数为空,它总是有助于检查模型状态是否存在错误。更多在这篇博文中下面的示例代码有帮助,

if (!ModelState.IsValid) 
{
    throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState);
}
于 2013-09-23T17:25:28.990 回答