2017-07-25 12 views
1

ルートコード404を返します。角度成分からPostメソッドは、以下のAsp.netコアウェブAPIコントローラ

コール:

onSubmit(employeeItems: any) {   
     console.log(employeeItems); 
     this.getData(); 
     var headers = new Headers(); 
     headers.append('Content-Type', 'application/json; charset=utf-8'); 
     this.http.post('api/Employee/Post', employeeItems, { headers: headers }).subscribe(); 
     this.createEmployeeFlag = false; 
    } 

私もポストマンから試してみましたが、ない幸運。

+1

https://docs.microsoft.com/en-us/aspnet/コア/ mvc/controllers/routing –

答えて

1

あなたのURLとルートテンプレートが

[Route("api/[controller]")] 
public class EmployeeController : Controller { 

    [HttpPost] 
    public async Task<IActionResult> Post([FromBody]Employee employee) { 
     await employeeManager.CreateAsync(employee); 
     return Ok(); 
    } 
} 

と一致してapi/Employee

onSubmit(employeeItems: any) {   
    console.log(employeeItems); 
    this.getData(); 
    var headers = new Headers(); 
    headers.append('Content-Type', 'application/json; charset=utf-8'); 
    this.http.post('api/Employee', employeeItems, { headers: headers }).subscribe(); 
    this.createEmployeeFlag = false; 
} 
+0

ありがとうございます! –

1

デフォルトのエンドポイントを呼び出すためにあなたを呼び出すURLを更新していないこれは、あなたのサービスに必要となるコードです。ここには2つの問題があります。最初はURLであり、完全なURLパスである必要があります。第二は、あなたは私が*.service.tsファイルにこれを壊すことをお勧めObservable

onSubmit(employeeItems: any) { 
    let url: string = 'http://localhost/api/employee'; //this will be the complete url that you would hit with say postman 
    this.getData(); //I'm not sure what this is so I'm leaving it here 
    this.http.post(url, employeeItems) 
     .map((response: Response) => response.json()) 
     .Subscribe((response: any) => { 
     //do whatever with the response here. 
     }); 
    this.createEmployeeFlag = false; 
} 

にそれをマッピングする前に、何かに加入しようとしているということです。あなたの*.component.ts

コンストラクタの内部

*.service.ts

public postEmployee(employeeItems: any): Observable<any> { 
    let url: string = 'http://localhost/api/employee'; //this will be the complete url that you would hit with say postman 
    this.http.post(url, employeeItems) 
    .map((response: Response) => response.json()); 
} 

(プライベートサービス:サービス){}

onSubmit(employeeItems: any) { 
    this.getData(); //I'm not sure what this is so I'm leaving it here 
    this.service.postEmployee(employeeItems) 
    .Subscribe((response: any) => { 
     //do whatever with the response here. 
    }); 
    this.createEmployeeFlag = false; 
} 
関連する問題