无法发布到EntitySetController,收到的实体为null

时间:2013-09-22 13:00:17

标签: asp.net post asp.net-web-api odata complextype

我正在尝试将数据发布到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"}

我注意到,如果我将versionZipCodeType字段(所有非引用字段)的引号用作"Version":"20""ZipCodeType":"3",那么一切都很好并且已接收实体是not null

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

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

此外,如果传入参数为null,则检查模型状态是否有帮助总是有帮助的。更多内容在此博客中post。下面的示例代码有帮助,

if (!ModelState.IsValid) 
{
    throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState);
}