Dotnet
Dotnet

Reputation: 63

Angular 8 - Can not call webapi

I am trying to get data from web api using parameter.i can't get data.no t showing error message also.

dash board component

Countrymodel: Country = { country_id: '', country_name: '', status: false } Countries: Observable;

constructor(private api: RestApiService, private router: Router) { }



ngOnInit() {
    let currentUserName = localStorage.getItem('currentUserName');
    console.log(currentUserName);
    this.GetCountry(currentUserName);

}
GetCountry(currentUserName: string) {
    this.Countries = this.api.getcountry(currentUserName);
    console.log(this.Countries);
}

}

api.service

   export class RestApiService {
Url = 'http://localhost:xxxx/Api/Example/';
header: any;
constructor(private http: HttpClient) {
    const headerSettings: { [name: string]: string | string[]; } = {};
    this.header = new HttpHeaders(headerSettings);
}
getcountry(currentUserName: string): Observable<Country[]> {
    console.log(this.Url + 'GetCountry?Username=' + currentUserName);
    return this.http.get<Country[]>(this.Url + 'GetCountry?Username=' + currentUserName);

}

WebApi

[Route("Api/Example/GetCountry")]
    [HttpGet]
    public IHttpActionResult GetCountry(string Username)
    {
    }

Upvotes: 0

Views: 100

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222582

You have to subscribe to the call if you want it to execute. See the HttpClient

this.api.getcountry(currentUserName).subscribe((data)=>{
    console.log(this.Countries);
});

Upvotes: 2

Related Questions