如何在 WEB 中将多个模型作为参数传递 API
How to pass multiple model as parameter in WEB API
谁能帮我将多个模型作为参数传递给 WEB 中的请求内容API?
我有 2 个不同的模范学生和员工
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public string Branch { get; set; }
}
public class Employee
{
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public string Department { get; set; }
}
我创建了一个 API 并想将这两个模型作为参数传递到我的操作方法 InsertTestAPI.
[HttpPost]
[Route("TestAPI")]
public HttpResponseMessage InsertTestAPI(Student modelStudent, Employee modelEmployee)
{
// other logical operations
}
当我在请求正文中将这些模型作为 JSON 传递时,我从 Postman 收到以下错误。
{
"$id": "1",
"Message": "An error has occurred.",
"ExceptionMessage": "Can't bind multiple parameters ('modelStudent' and 'modelEmployee') to the request's content.",
"ExceptionType": "System.InvalidOperationException",
"StackTrace": " at System.Web.Http.Controllers.HttpActionBinding.ExecuteBindingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)\r\n at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()"
}
谁能帮帮我?
之后可以在student class中调用Employee class,然后在api
中调用multiple model call
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public string Branch { get; set; }
public Employee EmpData {get;set;}
}
public class StudentEmployeModel
{
public Student Students{get;set;}
public Employee Employees{get;set;}
}
以这种方式创建 StudentEmpendedModel class。
public HttpResponseMessage InsertTestAPI(StudentEmployeModel model)
{// other logical operations }
通过这种方式请求
{ "students": { "studentId":"0", "studentName":"Mehmet", "branch":"software" } ,
"employees":
{"employeeId ":0,"employeeName":"Test","department":"IT"} }
通过这种方式,您可以将请求发送为 Json
谁能帮我将多个模型作为参数传递给 WEB 中的请求内容API?
我有 2 个不同的模范学生和员工
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public string Branch { get; set; }
}
public class Employee
{
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public string Department { get; set; }
}
我创建了一个 API 并想将这两个模型作为参数传递到我的操作方法 InsertTestAPI.
[HttpPost]
[Route("TestAPI")]
public HttpResponseMessage InsertTestAPI(Student modelStudent, Employee modelEmployee)
{
// other logical operations
}
当我在请求正文中将这些模型作为 JSON 传递时,我从 Postman 收到以下错误。
{
"$id": "1",
"Message": "An error has occurred.",
"ExceptionMessage": "Can't bind multiple parameters ('modelStudent' and 'modelEmployee') to the request's content.",
"ExceptionType": "System.InvalidOperationException",
"StackTrace": " at System.Web.Http.Controllers.HttpActionBinding.ExecuteBindingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)\r\n at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()"
}
谁能帮帮我?
之后可以在student class中调用Employee class,然后在api
中调用multiple model callpublic class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public string Branch { get; set; }
public Employee EmpData {get;set;}
}
public class StudentEmployeModel
{
public Student Students{get;set;}
public Employee Employees{get;set;}
}
以这种方式创建 StudentEmpendedModel class。
public HttpResponseMessage InsertTestAPI(StudentEmployeModel model)
{// other logical operations }
通过这种方式请求
{ "students": { "studentId":"0", "studentName":"Mehmet", "branch":"software" } ,
"employees":
{"employeeId ":0,"employeeName":"Test","department":"IT"} }
通过这种方式,您可以将请求发送为 Json