Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果angular 2中有多个Post方法,则调用web Api方法_Angular_Asp.net Web Api_Asp.net Web Api Routing - Fatal编程技术网

如果angular 2中有多个Post方法,则调用web Api方法

如果angular 2中有多个Post方法,则调用web Api方法,angular,asp.net-web-api,asp.net-web-api-routing,Angular,Asp.net Web Api,Asp.net Web Api Routing,我有一个web api,它有2个Post方法 我从angular 2调用该方法,但每次它调用第一个方法PostEmployee时。我在第二种方法中使用了route public IHttpActionResult PostEmployee(Employee employee) { if (!ModelState.IsValid) { return BadRequest(ModelState); }

我有一个web api,它有2个Post方法

我从angular 2调用该方法,但每次它调用第一个方法PostEmployee时。我在第二种方法中使用了route

  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);
    }
角度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的登录方法


谢谢

我确信您的WebApiConfig文件配置不正确

这些答案应该对您有所帮助:


谢谢你@riwex。