自动部署后,如何更改appsettings.json中的设置? [英] How can I change a setting in appsettings.json after auto-deploy?
问题描述
我正在运行一个ASP.NET Core应用程序,并在其上设置了Github自动部署功能.但是由于这是一个开放库,所以我显然不想上传正确的配置文件.
I have an ASP.NET Core application going on an have setup Github auto-deploy on it. But since it's an open repo I obviously don't want to upload my correct configuration file.
我想做的是在github自动部署后替换appsettings.json中的一些字符串.
What I'd like to do is to replace some strings in the appsettings.json after a github auto deploy.
"AppSettings": {
"Token": "my super duper secret token"
}
在Azure上部署github后,如何将my super duper secret token
更改为我的真实令牌?
How can I change my super duper secret token
to my real token after a github deploy on Azure?
推荐答案
据我所知,我们可以在Azure端口的应用程序设置"中配置令牌. 我对此进行了测试,它可以成功运行,以下是我的详细步骤.
As I know we can config token in App Settings on the Azure port. I do a test on this, it works successfully, the following is my detail steps.
- 创建一个Asp.net核心应用程序.
- 在appsetting.json文件中添加[AppSettings]部分(令牌值:mysecretkey ).
- 在创建的项目下添加一个公共类AppSettings.cs.
- 将代码
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"))
添加到Startup.cs文件(对于.net Core 1.0)中的功能ConfigureService函数中.
- Create an Asp.net core Application.
- Add [AppSettings] section in the appsetting.json file (Token vaule: mysecretkey).
- Add a public class AppSettings.cs under the created project.
- Add the code
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"))
in the function ConfigureService function in the Startup.cs file (For .net Core 1.0).
注:模型绑定的语法已从RC1更改为RC2.使用
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"))
不再可用为了将设置类绑定到您的配置,您需要在Startup.cs的ConfigureServices方法中进行配置:services.Configure<AppSettings>(options => Configuration.GetSection("AppSettings").Bind(options));
Note:The syntax for model binding has changed from RC1 to RC2. Using
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"))
, is no longer availableIn order to bind a settings class to your configuration you need to configure this in the ConfigureServices method of Startup.cs:services.Configure<AppSettings>(options => Configuration.GetSection("AppSettings").Bind(options));
5.将代码添加到HomeController.cs文件.
5. Add code to the HomeController.cs file.
- 将WebApp发布到Azure门户.
- 在Azure门户中添加[AppSettings:令牌].
- 浏览WebApp并选择关于"选项卡以查看令牌值是在门户中设置的值.
- Publish the WebApp to the Azure Portal.
- Add [AppSettings: Token] in the Azure Portal.
- Browse the WebApp and select the about tab to see the token value is that the value set in the portal.
这篇关于自动部署后,如何更改appsettings.json中的设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!