I have an ASP.NET Core 3.1 web application and I am trying to configure the app using parameters from appsettings.json. I am using the Options pattern to inject my configuration into my razor code-behind. Then, I paint the UI based on this configuration that is passed on as a model to the view. This part works fine. How do I pass this configuration to my javascript file? Do I expose an endpoint in my code-behind so the JavaScript sends an ajax request to this endpoint everytime before making an ajax request for data? For example:-
appsettings.json:
"ProjectOptions": {
"ProjectName": "UYHGJHG",
"ProjectClient": "TYRTYR",
"ProjectCustomer": "EWQD",
"UI": {
"Transaction": {
"EnableTransactionSearch": "yes",
"EnableTransactionDataRetrieval": "yes"
},
"Batch": {
"EnableBatchSummaryReport": "yes",
"EnableBatchDetailReport": "yes"
}
}
}
UIConfiguration.cs class:
public class UIConfiguration{
public string ProjectName { get; set; }
public string ProjectClient { get; set; }
public string ProjectCustomer { get; set; }
public UI UI { get; set; }
}
public class UI{...}
Startup.cs:
services.Configure<UIConfiguration>(Configuration.GetSection("ProjectOptions"));
index.cshtml.cs:
private readonly UIConfiguration UI_Configuration;
public IndexModel(IOptions<UIConfiguration> projectOptions){
UI_Configuration = projectOptions.value;
}
public MyConfiguration ViewConfiguration = new MyConfiguration();
public async Task<IActionResult> OnGet(){
ViewConfiguration = new MyConfiguration(){property1 = UI_Configuration.UI.Transaction.EnableTransactionSearch, ...};
return Page();
}