【问题标题】:web.config in ASP.NET Core 2ASP.NET Core 2 中的 web.config
【发布时间】:2018-10-24 20:51:24
【问题描述】:

我刚开始学习 ASP.NET Core 2 MVC 应用程序。

新建项目后,我在解决方案资源管理器中看不到任何 web.config 文件。

有什么帮助吗?

抱歉,这是一个愚蠢的问题。

【问题讨论】:

标签: web-config asp.net-core-2.0


【解决方案1】:

不仅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 &lt;project&gt;.csproj

web.config 将在您发布 Web 项目后生成。

【讨论】:

    猜你喜欢
    • 2018-10-03
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多