能否请您告诉我如何从“ Program.cs”中的appsettings.json获取值。如果我从这样的“ Startup.cs”开始:
模型
class AppConfiguration
{
public string UrlProperty { get; set; }
}
Startup.cs
services.AddSingleton(Configuration.GetSection("Url").Get<AppConfiguration>());
然后转到“ Program.cs”并按如下所示进行编码:
Program.cs
public static string Url { get; set; }
public static AppConfiguration configurtion { get; set; }
public Program(AppConfiguration configuration)
{
Url = configuration.ToString();
}
static void Main(string[] args)
{
CreateWebHostBuilder(args).Run();
}
public static IWebHost CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseKestrel((hostingContext, options) =>
{
var ipAddress = IPAddress.Parse(Url); // ERROR HERE
options.Listen(ipAddress, 9001);
}).Build();
由于先执行main
,然后执行CreateWebHostBuilder
,所以将出现错误,因此不会首先填充Url
。但是,如果我使用以下代码:
static void Main(string[] args)
{
var serviceProvider = new ServiceCollection()
.AddSingleton(Configuration.GetSection("Url").Get<AppConfiguration>());
CreateWebHostBuilder(args).Run();
}
Configuration.GetSection("Url")
无法识别。如何从Program.Main中的appsettings.json获取值?
根据“ Tan”,我对Program.cs
进行了必要的编辑。我想我快解决了。只是加载时“ Url”仍然为空。请查看我的修改:
static void Main(string[] args)
{
var builder = CreateWebHostBuilder(args);
builder.ConfigureLogging((context, builder) =>
{
Url = context.Configuration["ApplicationUrl"];
}).Build().Run();
}
public static IHostBuilder CreateWebHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>()
.UseKestrel((hostingContext, options) =>
{
var ipAddress = IPAddress.Parse(Url);
options.Listen(ipAddress, 9001);
options.Limits.MaxRequestBodySize = null;
});
});
请查看屏幕截图:
这是 appsettings.json
{
"AllowedHosts": "*",
"AcceptedOrigin": [ "http://localhost:4200" ],
"ApplicationUrl": "192.168.1.4"
}
这是 appsettings.json
{
"AllowedHosts": "*",
"AcceptedOrigin": [ "http://localhost:4200" ],
"ApplicationUrl": "192.168.1.4"
}
static void Main(string [] args) {
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>()
.UseKestrel((context, options) =>
{
options.Limits.MaxRequestBodySize = null;
// "192.168.1.4"
string url = context.Configuration["Url"];
var ipAddress = System.Net.IPAddress.Parse(url);
});
});
这就是我在Startup.cs
中得到的东西:
在Program.cs
中:
答案 0 :(得分:1)
在文件appsettings.json
中,您可以这样定义Url
属性:
{
"Url": "https://example.com"
}
在文件Program.cs
的{{1}}方法内,您可以通过Main
扩展方法访问该属性:
ConfigureLogging
更新:
如果要通过public class Program
{
public static void Main(string[] args)
{
var builder = CreateHostBuilder(args);
builder.ConfigureLogging((context, builder) => {
// "https://example.com"
string url = context.Configuration["Url"];
// do stuff...
}).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>()
.UseKestrel(options =>
{
options.Limits.MaxRequestBodySize = null;
});
});
}
扩展方法获取属性值,则可以直接分配它,而无需存储在字段中。
因为扩展方法完成后((UseKestrel
将成为扩展方法之外的Url
)会丢失该值
null
屏幕截图: