我刚开始使用Blazor (Web程序集),并将其链接到一个API。所以这确实是一个设计/代码问题。
我想配置一个测试和生产URL (连接到API),不确定我是否做了正确的/最佳实践,所以任何示例都会很好吗?(我很肯定我做错了!)
我想我应该在Blazor项目中创建一个JSON文件,并且只需要一个生产和测试URL。我是从program.cs开始的:
public class Program
{
private static string ApiConfig { get; set; }
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
LoadJsonConfiguration();
builder.Services.AddHttpClient<INotificationDataService, NotificationDataService>(client =>
client.BaseAddress = new Uri("ApiConfig"));
await builder.Build().RunAsync();
}
public static void LoadJsonConfiguration()
{
var config = JObject.Parse(@"/Config/application.json");
using (StreamReader file = File.OpenText(@"/Config/application.json"))
using (JsonTextReader reader = new JsonTextReader(file))
{
var a = reader.Read();
ApiConfig = "ReadConfig here??";
}
}
} application.JSON文件
{
"Url": {
"LocalTest": "https://localhost:44302",
"Production": "https://Todo"
}
}发布于 2020-10-26 19:26:45
在.json文件夹中创建两个wwwroot文件,注意名称:
appsettings.json
{
"Url": "https://Todo""
}appsettings.Development.json
{
"Url": "https://localhost:44302"
}在Program.cs中:
var url = builder.Configuration.GetSection("Url").Value;
...根据ASPNETCORE_ENVIRONMENT的不同,将加载相关的url。
https://stackoverflow.com/questions/64543185
复制相似问题