如果 2 个函数具有相同的参数类型,如何从 javascript 调用 webapi
how to call a webapi from javascript if the 2 functions has same arguments types
我是网络新手API,尝试通过谷歌搜索和搜索来学习。
我能够完成与 API 一起正常工作的 CRUD。我从 Javascript 调用这个 WebAPI,它工作正常。
但是我有这种情况。
Emplyee 和 EmployeeTime
员工包含:ID、用户名、姓名
EmployeeTime 包含:ID、DateIn、Employee_Id
使用 EmployeeController 我可以做到 update/delete/Add -> 我正在使用 ID。
现在有了 EmployeeTimeController -> 我也可以做到 update/Add/Delete。
但是我怎样才能对 EmployeeTimeController 进行 CRUD 操作,by "Employee_Id"?
public async Task<IHttpActionResult> GetEmployeeTime(int id)
{
TimeAttendance timeattendance = await db.TimeAttendances.FindAsync(id);
if (timeattendance == null)
{
return NotFound();
}
return Ok(timeattendance);
}
如您所见,参数中的 ID 与 EmployeeTime.Id 的 ID 相关联。
我想通过传递 Employee.Employee_Id
来获取所有记录
public async Task<IHttpActionResult> GetEmployeeTimeByEmployee(int employeeId)
{
TimeAttendance timeattendance = dbContext.EmployeeTime.Where(e=> e.Employee_Id == employeeId).ToList<timeattendance>();
if (timeattendance == null)
{
return NotFound();
}
return Ok(timeattendance);
}
简而言之:
当我从 javascript 打电话时:
//it returns employee with employee.ID 2
http://localhost:24374/api/employees/2
//it returns employeeTime RECORD with employeeTime.ID 4
http://localhost:24374/api/employeesTime/4
//How do I get all records from employeeTime which employee_Id = 4
http://localhost:24374/api/employeesTime/??????
我想将 Employee_Id 值传递给 "EmployeeTimeController",但由于两者具有相同的参数类型 (INT),我该如何区分?
我可以在 Controller 中创建一个具有不同名称的新函数,但是如何从 javascript 调用?
我会在名为 GetEmployeeTime 的员工控制器中放置一个方法,您可以使用路由属性对其进行配置:
[HttpGet]
[Route("api/employees/{id}/Time")]
public YourModel GetEmployeeTime(int id){
Your code here
}
然后您可以使用 url:
通过 JS 调用它
http://localhost:24374/api/employees/2/Time
我是网络新手API,尝试通过谷歌搜索和搜索来学习。
我能够完成与 API 一起正常工作的 CRUD。我从 Javascript 调用这个 WebAPI,它工作正常。
但是我有这种情况。
Emplyee 和 EmployeeTime
员工包含:ID、用户名、姓名
EmployeeTime 包含:ID、DateIn、Employee_Id
使用 EmployeeController 我可以做到 update/delete/Add -> 我正在使用 ID。
现在有了 EmployeeTimeController -> 我也可以做到 update/Add/Delete。 但是我怎样才能对 EmployeeTimeController 进行 CRUD 操作,by "Employee_Id"?
public async Task<IHttpActionResult> GetEmployeeTime(int id)
{
TimeAttendance timeattendance = await db.TimeAttendances.FindAsync(id);
if (timeattendance == null)
{
return NotFound();
}
return Ok(timeattendance);
}
如您所见,参数中的 ID 与 EmployeeTime.Id 的 ID 相关联。
我想通过传递 Employee.Employee_Id
来获取所有记录 public async Task<IHttpActionResult> GetEmployeeTimeByEmployee(int employeeId)
{
TimeAttendance timeattendance = dbContext.EmployeeTime.Where(e=> e.Employee_Id == employeeId).ToList<timeattendance>();
if (timeattendance == null)
{
return NotFound();
}
return Ok(timeattendance);
}
简而言之: 当我从 javascript 打电话时:
//it returns employee with employee.ID 2
http://localhost:24374/api/employees/2
//it returns employeeTime RECORD with employeeTime.ID 4
http://localhost:24374/api/employeesTime/4
//How do I get all records from employeeTime which employee_Id = 4
http://localhost:24374/api/employeesTime/??????
我想将 Employee_Id 值传递给 "EmployeeTimeController",但由于两者具有相同的参数类型 (INT),我该如何区分?
我可以在 Controller 中创建一个具有不同名称的新函数,但是如何从 javascript 调用?
我会在名为 GetEmployeeTime 的员工控制器中放置一个方法,您可以使用路由属性对其进行配置:
[HttpGet]
[Route("api/employees/{id}/Time")]
public YourModel GetEmployeeTime(int id){
Your code here
}
然后您可以使用 url:
通过 JS 调用它http://localhost:24374/api/employees/2/Time