Dotnet webapi项目中get请求后对象数据为空
我创建了两个模型类 Organization.cs 和 Employee.cs
public class Organisation
{
private static string organisationName="FALCON";
private int employeeCount=2;
private string ceoName;
private List<string> departmentNames;
[JsonIgnore]
private List<Employee> employees;
private List<Address> addesses;
private bool isCeoChangeAvailable= true;
public string OrganisationName
{
get {return organisationName;}
set {organisationName = value;}
}
public int EmployeeCount
{
get {return employeeCount;}
set { employeeCount = value; }
}
public string CeoName
{
get { return ceoName; }
set { ceoName = value; }
}
public List<string> Departments
{
get { return departmentNames; }
set { departmentNames = value; }
}
[IgnoreDataMember]
public List<Employee> Employees
{
get {return employees; }
set {employees=value; }
}
public List<Address> OfficeAdresses
{
get {return addesses; }
set { addesses = value; }
}
public bool IsCeoChange
{
get{ return isCeoChangeAvailable; }
set { isCeoChangeAvailable = value; }
}
public Organisation(string ceoName,List<string> departments,List<Address> addresses)
{
this.CeoName = ceoName;
this.Departments = departments;
this.OfficeAdresses = addresses;
}
public Organisation() { }
}
,并且员工类
public class Employee
{
private Guid employeeId;
private string name;
private string departmentName;
private bool isManager;
private int salary;
public Guid Id
{
get
{ return employeeId;}
set
{ employeeId = value;}
}
public string Name
{
get
{ return name;}
set
{ name = value;}
}
public bool IsManager
{
get
{ return isManager;}
set
{ isManager = value;}
}
public string DepartmentName
{
get { return departmentName;}
set { departmentName = value;}
}
public int Salary
{
get { return salary; }
set { salary = value; } }
public Employee()
{
}
}
有两个用于员工和组织的控制器,我试图发布组织数据并将其存储在变量中。
在我的组织控制器中,我有这个方法
public IActionResult PostOrganisationData(Organisation organisation)
{
_organisationData.PostOrganisationDetails(organisation);
return Created(HttpContext.Request.Scheme + "://" + HttpContext.Request.Host + HttpContext.Request.Path + "/" + organisation.OrganisationName, organisation);
}
在我的数据层中有一个类 MockEmployeeData ,它实现 IOrganizationData 接口 这是我构建 Organization 对象并将其分配给 Organization 对象的实例的代码。在请求期间组织对象保存分配的数据。下面是代码
private Organisation organisation = new Organisation();
public Organisation PostOrganisationDetails(Organisation organisation)
{
Organisation organisationObject = new Organisation(organisation.CeoName, organisation.Departments, organisation.OfficeAdresses);
organisation = organisationObject;
return organisation;
}
当我调用 Get 操作方法从控制器获取组织详细信息时,先前分配的对象具有 null 属性。这是 get 方法
[HttpGet]
[Route("api/[controller]")]
public IActionResult GetOrganisationDetails()
{
var organisationData=_organisationData.GetOrganisationData();
return Ok(organisationData);
}
public Organisation GetOrganisationData()
{
Organisation organisationObject = new Organisation();
organisationObject = organisation;
return organisationObject;
}
在这个方法中,我分配一个变量,该变量保存之前从发布请求分配的数据,并返回所有属性现在都变为空的对象。请帮我解决这个问题
I have created two model classes Organisation.cs and Employee.cs
public class Organisation
{
private static string organisationName="FALCON";
private int employeeCount=2;
private string ceoName;
private List<string> departmentNames;
[JsonIgnore]
private List<Employee> employees;
private List<Address> addesses;
private bool isCeoChangeAvailable= true;
public string OrganisationName
{
get {return organisationName;}
set {organisationName = value;}
}
public int EmployeeCount
{
get {return employeeCount;}
set { employeeCount = value; }
}
public string CeoName
{
get { return ceoName; }
set { ceoName = value; }
}
public List<string> Departments
{
get { return departmentNames; }
set { departmentNames = value; }
}
[IgnoreDataMember]
public List<Employee> Employees
{
get {return employees; }
set {employees=value; }
}
public List<Address> OfficeAdresses
{
get {return addesses; }
set { addesses = value; }
}
public bool IsCeoChange
{
get{ return isCeoChangeAvailable; }
set { isCeoChangeAvailable = value; }
}
public Organisation(string ceoName,List<string> departments,List<Address> addresses)
{
this.CeoName = ceoName;
this.Departments = departments;
this.OfficeAdresses = addresses;
}
public Organisation() { }
}
and an employee class
public class Employee
{
private Guid employeeId;
private string name;
private string departmentName;
private bool isManager;
private int salary;
public Guid Id
{
get
{ return employeeId;}
set
{ employeeId = value;}
}
public string Name
{
get
{ return name;}
set
{ name = value;}
}
public bool IsManager
{
get
{ return isManager;}
set
{ isManager = value;}
}
public string DepartmentName
{
get { return departmentName;}
set { departmentName = value;}
}
public int Salary
{
get { return salary; }
set { salary = value; } }
public Employee()
{
}
}
have a two controllers for Employee and Organisation, I am trying to post Organisation data and storing it in a variable.
In my organisaiton controller I have this method
public IActionResult PostOrganisationData(Organisation organisation)
{
_organisationData.PostOrganisationDetails(organisation);
return Created(HttpContext.Request.Scheme + "://" + HttpContext.Request.Host + HttpContext.Request.Path + "/" + organisation.OrganisationName, organisation);
}
In my datalayer have a class MockEmployeeData which implements IOrganisationData interface
here is the code where I am constructing the Organisation object and assigning it to an instance of Organisation object. During the request The organisation object holds the assigned data. Here is the code
private Organisation organisation = new Organisation();
public Organisation PostOrganisationDetails(Organisation organisation)
{
Organisation organisationObject = new Organisation(organisation.CeoName, organisation.Departments, organisation.OfficeAdresses);
organisation = organisationObject;
return organisation;
}
When I call the Get action method which gets the organisation details from the controller the previously assigned object has properties with null. Here is the get method
[HttpGet]
[Route("api/[controller]")]
public IActionResult GetOrganisationDetails()
{
var organisationData=_organisationData.GetOrganisationData();
return Ok(organisationData);
}
public Organisation GetOrganisationData()
{
Organisation organisationObject = new Organisation();
organisationObject = organisation;
return organisationObject;
}
Here in this method I,m assigning the variable which holds the previously assigned data from post request and getting back the object where all properties have gone null now.Please help me solve this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将
Organization
模型类中的属性设置为可为空。现在,您可以通过手动使类中的每个属性可为空来实现此目的:或者您可以通过编辑包含项目属性的
csproj
文件来全局启用此属性:You need to make your properties in your
Organisation
model class to nullable. Now you can do this by either manually making each property nullable in your class:Or you can globally enable this property by editing your
csproj
file which contains your project properties: