I have a service which requests some JSON data from a Web API:
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class ReviewService {
private actionUrl: string;
constructor(private _http: Http) {
this.actionUrl = 'http://api.dc-shop.com/review';
}
public GetAll = (): any => {
return this._http.get(this.actionUrl)
.map(x => x.json());
}
}
I would like to avoid hard coding the API address in the constructor and instead read a setting from appSettings.json
which I can use for the action URL at startup for production and localhost servers.
What is the best way of doing this in ASP.NET MVC Core?
Two options I can think of:
Put the configuration in environment.ts
as well as appSettings.json
- access to this is built into Angular.
Step 1 of your service loads appSettings.json
using a straight-forward http.get
and then uses the config from it load the required data.
Example of environment.ts
export const environment = {
apiUrl: 'http://api.dc-shop.com',
mode: 'prod'
};
Example usage:
import { environment } from './environment';
constructor(private _http: Http) {
this.actionUrl = environment.apiUrl + '/review';
}