【问题标题】:Cannot post to EntitySetController, the received entity is null无法发布到 EntitySetController,接收到的实体为空
【发布时间】:2013-09-22 13:00:16
【问题描述】:

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

邮编是这样定义的:

 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

请帮我解决这个问题。

【问题讨论】:

  • 确保您的请求有 `Content-Type' 标头。

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


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多