我有一个web api,它有2个Post方法。
我从angular 2调用该方法,但每次调用第一个方法(PostEmployee)。我在第二种方法上使用过路线。
public IHttpActionResult PostEmployee(Employee employee)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Employees.Add(employee);
return CreatedAtRoute("DefaultApi", new { id = employee.EmpID }, employee);
}
[Route("Login")]
public IHttpActionResult Login(string username, string password)
{
Employee emp = db.Employees.FirstOrDefault(t=>t.EmpName == username && t.Address == password);
return CreatedAtRoute("DefaultApi", new { id = emp.EmpID }, emp);
}
Angular 2服务代码:
login(username: string, password: string) {
debugger
let headers = new Headers({ 'Content-Type': 'application/json', 'Accept': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.post('http://localhost:49221/api/Employee/Login', { username: username, password: password }, headers)
.map((response: Response) => {
let user = response.json();
if (user && user.token) {
// store user details and jwt token in local storage to keep user logged in between page refreshes
localStorage.setItem('currentUser', JSON.stringify(user));
}
});
}
create(employee: Employee) {
let headers = new Headers({ 'Content-Type': 'application/json', 'Accept': 'application/json' });
let options = new RequestOptions({ headers: headers });
// let body = JSON.stringify(employee);
return this.http.post('http://localhost:49221/api/Employee', employee, headers).map((res: Response) => res.json());
}
登录和创建服务方法都调用了Web api的PostEmployee。
如何从服务中调用web api的登录方法?
由于