ASP.NET核心Web API-当类的成员是接口列表时,参数绑定不起作用 公共异步任务AddProjectDetail(ProjectViewModel项目) { // } 公共类ProjectViewModel:IProjectViewModel { 公共字符串Id{get;set;} public IKeyValue UOPSegment{get;set;}=new KeyValue(); 公共IList ProjectGates{get;set;}//未在此处绑定数据 } 公共接口IProjectViewModel:IBaseDBModel { 字符串Id{get;set;} IKeyValue UOPSegment{get;set;} IList ProjectGates{get;set;} } 公共接口IProjectGateAssocViewModel { 字符串网关代码{get;set;} 字符串GateDate{get;set;} 字符串ProjectId{get;set;} } 公共类项目GateAssocViewModel:IProjectGateAssocViewModel { 公共字符串网关代码{get;set;} 公共字符串GateDate{get;set;} 公共字符串ProjectId{get;set;} }

ASP.NET核心Web API-当类的成员是接口列表时,参数绑定不起作用 公共异步任务AddProjectDetail(ProjectViewModel项目) { // } 公共类ProjectViewModel:IProjectViewModel { 公共字符串Id{get;set;} public IKeyValue UOPSegment{get;set;}=new KeyValue(); 公共IList ProjectGates{get;set;}//未在此处绑定数据 } 公共接口IProjectViewModel:IBaseDBModel { 字符串Id{get;set;} IKeyValue UOPSegment{get;set;} IList ProjectGates{get;set;} } 公共接口IProjectGateAssocViewModel { 字符串网关代码{get;set;} 字符串GateDate{get;set;} 字符串ProjectId{get;set;} } 公共类项目GateAssocViewModel:IProjectGateAssocViewModel { 公共字符串网关代码{get;set;} 公共字符串GateDate{get;set;} 公共字符串ProjectId{get;set;} },asp.net,class,interface,core,webapi,Asp.net,Class,Interface,Core,Webapi,//公共IList项目门{get;set;} 这里的数据没有绑定。 当我尝试调用api时,我发现错误如下 {“项目文件[0]。内容类型”:[ “输入无效。”]} 问题是您希望ASP.NET核心绑定能够实例化接口对象。这是不可能的。或者您希望ASP.NET核心绑定能够猜测您希望为给定接口实例化哪种类型。这是可能的,但恐怕没有人认为这是有效的绑定场景 要理解绑定的问题,您需要了解它是如何工作的。绑定能够使用公共无参数构造函数和可写属性、集合和字典绑定简单类型、复杂类型 简单类型() 模型绑定器可以将

//公共IList项目门{get;set;}

这里的数据没有绑定。 当我尝试调用api时,我发现错误如下

{“项目文件[0]。内容类型”:[ “输入无效。”]}


问题是您希望ASP.NET核心绑定能够实例化接口对象。这是不可能的。或者您希望ASP.NET核心绑定能够猜测您希望为给定接口实例化哪种类型。这是可能的,但恐怕没有人认为这是有效的绑定场景

要理解绑定的问题,您需要了解它是如何工作的。绑定能够使用公共无参数构造函数和可写属性、集合和字典绑定简单类型、复杂类型

简单类型()

模型绑定器可以将源字符串转换为以下简单类型:

Boolean
Byte
SByte
Char
DateTime
DateTimeOffset
Decimal
Double
Guid
Int16
Int32
Int64
Single
TimeSpan
UInt32
UInt64
Uri
版本

复杂类型()

复杂类型必须具有要绑定的公共默认构造函数和公共可写属性。发生模型绑定时,使用公共默认构造函数实例化该类

对于复杂类型的每个属性,模型绑定都会在源中查找名称模式前缀.property\u name。如果未找到任何内容,则只查找不带前缀的属性名称

对于绑定到参数,前缀是参数名称。对于绑定到PageModel公共属性,前缀是公共属性名称。某些属性具有Prefix属性,可用于覆盖参数或属性名称的默认用法

收藏()

对于简单类型集合的目标,模型绑定会查找与参数\u name或属性\u name的匹配项。如果未找到匹配项,它将查找不带前缀的受支持格式之一

字典()

对于字典目标,模型绑定将查找与参数\名称或属性\名称的匹配项。如果未找到匹配项,它将查找不带前缀的受支持格式之一

附加内容(未记录)

到目前为止,ASP.NET核心绑定非常好,能够为您绑定到
IEnumerable
IDictionary

选项1

避免绑定到接口。你会省去很多挣扎

public async Task<IActionResult> AddProjectDetail(ProjectViewModel project)
{
//
}    

public class ProjectViewModel : IProjectViewModel
        {
           public string Id { get; set; }
           public IKeyValue UOPSegment { get; set; } = new KeyValue();
           public IList<IProjectGateAssocViewModel> ProjectGates { get; set; } //Data is not getting binded here
         }
    public interface IProjectViewModel : IBaseDBModel
        {
            string Id { get; set; }
            IKeyValue UOPSegment { get; set; }
            IList<IProjectGateAssocViewModel> ProjectGates { get; set; }
        }
    public interface IProjectGateAssocViewModel 
        {
            string GateCode { get; set; }
            string GateDate { get; set; }
            string ProjectId { get; set; }
        }

    public class ProjectGateAssocViewModel : IProjectGateAssocViewModel
        {
            public string GateCode { get; set; }
            public string GateDate { get; set; }
            public string ProjectId { get; set; }

        }
公共类ProjectViewModel:IProjectViewModel
{
公共字符串Id{get;set;}
公共IList ProjectGates{get;set;}//数据现在绑定到这里
}
公共接口IProjectViewModel
{
字符串Id{get;set;}
IList ProjectGates{get;set;}
}
公共接口IProjectGateAssocViewModel
{
字符串网关代码{get;set;}
字符串GateDate{get;set;}
字符串ProjectId{get;set;}
}
公共类项目GateAssocViewModel:IProjectGateAssocViewModel
{
公共字符串网关代码{get;set;}
公共字符串GateDate{get;set;}
公共字符串ProjectId{get;set;}
}
选项2

在需要首先实例化对象时,实现自定义绑定器、自定义类型转换器或自定义格式设置器

很难说您应该使用哪种方法,因为这取决于您提出的请求以及如何处理这些请求

public class ProjectViewModel : IProjectViewModel
{
    public string Id { get; set; }
    public IList<ProjectGateAssocViewModel> ProjectGates { get; set; } //Data are getting binded here now
}

public interface IProjectViewModel
{
    string Id { get; set; }
    IList<ProjectGateAssocViewModel> ProjectGates { get; set; }
}
public interface IProjectGateAssocViewModel
{
    string GateCode { get; set; }
    string GateDate { get; set; }
    string ProjectId { get; set; }
}

public class ProjectGateAssocViewModel : IProjectGateAssocViewModel
{
    public string GateCode { get; set; }
    public string GateDate { get; set; }
    public string ProjectId { get; set; }

}