Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
795 views
in Technique[技术] by (71.8m points)

asp.net - Passing Endpoint value from server config to Angular 9 app

I'm working with an Asp.Net Core 3 web application implementing an Angular 9 application. And I'm needing to pass a Url (host for the web api) found in the config file to the angular application. What is the best way to do this? One thing I thought of was to use an MVC page to write the value in a hidden input field, but I'm not sure if that is best practice. Thanks for the help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

My recommendation is the following, if you have the config file is a json you can make a get to it using the HTTPClient library of angular, after capturing the data you want you can create an Observer, BehaviorSubject, or whatever you feel more comfortable, in which you set the variable where you store the url, that way you can have the url accessible throughout the application

for example create service to get configFile

@Injectable({providedIn: 'root'})
export class ConfigLoaderService{

  constructor(private http: HttpClient) {}

  getConfigFile(): Observable<HttpResponse<any>> {
   return this.http.get<any>(`` + '/asset/config.json', { observe: 'response'});
  }
}

then yo can use this class in injecting in constructor

Hope help you, Regards


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...