web.config in ASP.NET Core 2

后端 未结 1 354
北恋
北恋 2021-01-07 06:30

I just started learning ASP.NET Core 2 MVC application.

After taking a new project, I don\'t see any web.config file in solution explorer.

Any help?

相关标签:
1条回答
  • 2021-01-07 07:07

    Not just Asp.Net Core 2.x no longer uses web.config, even the Asp.Net Core no longer uses it.

    The configuration now is part of the application startup procedure, in Startup.cs. And there is an application setting file called appsettings.json where you can put all your configuration values there.

    You can read setting values like this:

    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")
           });
        }
    }
    

    Also in .Net Core projects, there is .csproj file. You can right click a project and click Edit <project>.csproj.

    web.config will be generated after you publish your web projects.

    0 讨论(0)
提交回复
热议问题