在WebAPI 2 post方法中允许类属性为空

时间:2022-09-15 12:54:34

I am using two POCO classes:

我正在使用两个POCO类:

public class GearModel
{
    public long Id { get; set; }
    public Int16 LocationNumber { get; set; }
    public string HooksPerRod { get; set; }
    public string TerminalWeightBait { get; set; }
    public string TerminalWeight { get; set; }
    public FlyModel Shrimps { get; set; }
    public FlyModel Worms { get; set; }
    public FlyModel Cocahoes { get; set; }
    public FlyModel Scampi { get; set; }
}

public class FlyModel
{
    public double? Number { get; set; }
    public string Color { get; set; }
}

When I try to posts to my API Controller (defined below), it Complains Null value for non-nullable member. Member: 'Worms'., when using this cURL command to post to it (I know none of the cURL parameters are the issue, if I include all the FlyModel properties it works).

当我尝试向我的API控制器(定义如下)发布时,它会为不可空成员报错空值。成员:“蠕虫”。,当使用cURL命令将其发布到它(我不知道cURL参数是问题时,如果我包含了所有的FlyModel属性)。

Not Working

不工作


curl -X POST -v -H "Content-Type: application/json" --data-ascii "{Assn:102,LocationNumber:1,HooksPerRod:3,TerminalWeightBait:'None',TerminalWeight:'18oz',Shrimps:{Number:0.0,Color:'Pink'}}" <Url Here>

“内容类型:应用程序/json”——数据ascii“{Assn:102,LocationNumber:1,HooksPerRod:3,终止权重:‘None’,终止权:‘18oz’,Shrimps:{Number:0.0,Color:'Pink'}}” 这里是>

Working

工作


curl -X POST -v -H "Content-Type: application/json" --data-ascii "{Assn:102,LocationNumber:1,HooksPerRod:3,TerminalWeightBait:'None',TerminalWeight:'18oz',Shrimps:{Number:0.0,Color:'Pink'},Worms:{},Cocahoes:{},Scampi:{}}" <Url Here>

curl -X POST -v -H“Content-Type: application/json”-data-ascii“{Assn:102,LocationNumber:1,HooksPerRod:3,TerminalWeightBait:'None',TerminalWeight:'18oz',Shrimps:{Number:0.0,Color:'Pink'}, worm:{}, {},Cocahoes:

So my question is, how do I allow the FlyModel properties (Shrimps, Worms, etc) to be completely left out of the JSON like in the not working cURL command?

因此,我的问题是,我如何允许FlyModel属性(虾、蠕虫等)像not working cURL命令那样完全脱离JSON ?

GearController

GearController


public class GearController : ApiController
{
    // POST: api/Gear
    [ResponseType(typeof(GearModel))]
    public async Task<IHttpActionResult> PostGearModel(GearModel gearModel)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.Gear.Add(gearModel);
        await db.SaveChangesAsync();

        return CreatedAtRoute("DefaultApi", new { id = gearModel.Assn }, gearModel);
    }
}

1 个解决方案

#1


1  

I assume you are referring to the error after db.SaveChangesAsync();which is by Entity Framework, Web Api does not change anything here. The error happens because EF does not allow complex types nullability, that means you need to instantiate every complex type Shrimps, Worms... before saving.

我假设您指的是db.SaveChangesAsync()之后的错误;通过实体框架,Web Api在这里没有改变任何东西。错误的发生是因为EF不允许复杂类型的可空性,这意味着您需要实例化每个复杂类型的小虾、蠕虫……在储蓄。

You can read more about this convention at: Complex Types of Nullable Values or Complex Types Nullability Convention

您可以在:复杂类型的可空值或复杂类型的可空性约定中阅读有关此约定的更多信息

#1


1  

I assume you are referring to the error after db.SaveChangesAsync();which is by Entity Framework, Web Api does not change anything here. The error happens because EF does not allow complex types nullability, that means you need to instantiate every complex type Shrimps, Worms... before saving.

我假设您指的是db.SaveChangesAsync()之后的错误;通过实体框架,Web Api在这里没有改变任何东西。错误的发生是因为EF不允许复杂类型的可空性,这意味着您需要实例化每个复杂类型的小虾、蠕虫……在储蓄。

You can read more about this convention at: Complex Types of Nullable Values or Complex Types Nullability Convention

您可以在:复杂类型的可空值或复杂类型的可空性约定中阅读有关此约定的更多信息