web.config 在 ASP.NET 核心 2
web.config in ASP.NET Core 2
我刚开始学习ASP.NET Core 2 MVC 应用程序。
开始新项目后,我在解决方案资源管理器中没有看到任何 web.config 文件。
有什么帮助吗?
对不起,如果这是一个愚蠢的问题。
不仅Asp.Net Core 2.x
不再使用web.config
,就连Asp.Net Core
也不再使用
配置现在是 Startup.cs
中应用程序启动过程的一部分。还有一个名为 appsettings.json
的应用程序设置文件,您可以在其中放置所有配置值。
您可以这样读取设置值:
appsettings.json
{
"Recaptcha": {
"SiteKey": "xxxx-xxxx",
"SecretKey": "xxxx-xxxx"
}
}
Startup.cs
public class Startup
{
public IConfiguration Configuration { get; private set; }
public Startup(IConfiguration configuration)
{
this.Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
...
// Configure Google Recaptcha
// reading values from appsettings.json
services.AddRecaptcha(new RecaptchaOptions
{
SiteKey = this.Configuration.GetValue<string>("Recaptcha:SiteKey"),
SecretKey = this.Configuration.GetValue<string>("Recaptcha:SecretKey")
});
}
}
同样在.Net Core
个项目中,有.csproj
个文件。您可以右键单击项目并单击 Edit <project>.csproj
。
web.config
将在您发布 web 项目后生成。
我刚开始学习ASP.NET Core 2 MVC 应用程序。
开始新项目后,我在解决方案资源管理器中没有看到任何 web.config 文件。
有什么帮助吗?
对不起,如果这是一个愚蠢的问题。
不仅Asp.Net Core 2.x
不再使用web.config
,就连Asp.Net Core
也不再使用
配置现在是 Startup.cs
中应用程序启动过程的一部分。还有一个名为 appsettings.json
的应用程序设置文件,您可以在其中放置所有配置值。
您可以这样读取设置值:
appsettings.json
{
"Recaptcha": {
"SiteKey": "xxxx-xxxx",
"SecretKey": "xxxx-xxxx"
}
}
Startup.cs
public class Startup
{
public IConfiguration Configuration { get; private set; }
public Startup(IConfiguration configuration)
{
this.Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
...
// Configure Google Recaptcha
// reading values from appsettings.json
services.AddRecaptcha(new RecaptchaOptions
{
SiteKey = this.Configuration.GetValue<string>("Recaptcha:SiteKey"),
SecretKey = this.Configuration.GetValue<string>("Recaptcha:SecretKey")
});
}
}
同样在.Net Core
个项目中,有.csproj
个文件。您可以右键单击项目并单击 Edit <project>.csproj
。
web.config
将在您发布 web 项目后生成。