When attempting to call a URL from Angular 2 using http.get(), an exception is being thrown and the debugger in the controller is not being hit. Although I have checked the proxy and routing, they are the same as my existing projects. This new project is causing issues.
This project mainly consists of Angular UI with backend web API calls to fetch data, similar to other projects in the solution.
Code snippet:
Component.ts
ngOnInit() {
this.showpopup = true;
this.getContent();
}
getContent() {
this.myService.getContent()
.subscribe(
response => {
this.showpopup = false;
this.contentList = response;
},
err => {
this.showpopup = false;
this.displayAlert("Error getting content.", this.errorAlert);
}
);
}
Service.ts
import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions } from '@angular/http';
import { MyProject} from '../models/interfaces/server-interfaces';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
export class MyService{
public BASE_URL = 'api/myApp/';
constructor(public http: Http) { }
getContent(): Observable<MyProject.ProjectContent[]> {
return this.http.get(this.BASE_URL + 'ProjectContentList')
.map(response => response.json())
.catch(err => Observable.throw(err));
}
}
Controller.cs
[RoutePrefix("api/myApp")]
public class MyAppControllerController : ApiController
{
[Route("ProjectContentList")]
[HttpGet]
public async Task<HttpResponseMessage> GetContentAsync()
{
HttpResponseMessage response;
try
{
//code implementation here
}
catch (Exception ex)
{
//handling exceptions here
}
return response;
}
}
I am new to Angular. Is there something I am overlooking? Any assistance would be greatly appreciated.