如何重写之前发布基于构建配置文件?重写、配置文件

2023-09-13 05:16:32 作者:迩乱了俄的夏天

我有一个新的asp.net应用V5是真的只是一个静态的角度网站,我想的web.config文件转换做等价的。我app.js文件中有定义的是指向我们的API的URL的serviceUrl常数(不在同一网站)

I have a new asp.net V5 application that is really just a static angular site and I'm trying to do the equivalent of web.config transforms. My app.js file has a constant defined that is the serviceUrl which points to the url of our api (not in the same site)

我用大口替换和创建的任务为我们每一个构建目标是成功替代了需要更换的字符串。

I've used gulp-replace and created tasks for each of our build targets that successfully replace the single string that needs to be replaced.

我遇到的问题是,我需要能够改变基于选择这样我们就可以自动部署等,而不必更改URL为每个不同的目标构建配置该服务的URL。 (即开发去devapi.xxx.com和释放进入 https://api.xxx.com 等。 )

The problem I'm having is that I need to be able to change that service url based on the build configuration that is chosen so that we can automate deploys etc. and not have to change that url for each different target. (i.e. dev goes to devapi.xxx.com and release goes to https://api.xxx.com etc.)

我已经找到了prepublish脚本部分,但它似乎并没有让基于配置我要具体。并配置部分似乎并没有让我指定要运行脚本。

I've found the prepublish scripts section, but it doesn't seem to allow me to be specific based on the configuration. And the configurations section doesn't seem to allow me to specify scripts that will run.

所以我的问题是,一个人如何做一个pre-发布的步骤,查找和替换上下文的基础上配置该字符串?

So my question is, how does one do a pre-publish step that finds and replaces that string contextually based on the configuration?

推荐答案

您应该看一看多种环境的工作。

基本上,你的 appsettings.json 是你在找什么。在你的 Startup.cs 你应该有一个行有点儿像这样:

Basically, your appsettings.json is what you are looking for. In your Startup.cs you should have a line that kinda look like this:

var builder = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

在默认情况下,在Visual Studio中, ASPNET_ENV 环境变量将被设置为发展。当不使用Visual Studio中运行,它会默认为生产

By default, in Visual Studio, the ASPNET_ENV environment variable will be set to Development. When not run by Visual Studio, it will default to Production.

因此​​,通过定义,在生产环境中运行你的应用程序时,它会自动覆盖 appsettings.json 值与 appsettings.Production.json

So by definition, when running your app in production it will automatically override appsettings.json values with appsettings.Production.json.

该文件当然会是可选的,可以通过您的IT部门与它内部的所有的precious密码被保留。

That file will of course be optional and could be kept by your IT department with all the precious passwords inside of it.

如果你存储密码和连接系统,串不过,我会强烈建议阅读的安全存储应用程序的秘密的。

If you are storing passwords and connnection strings however, I would highly recommend reading about Safe Storage of Application Secrets.