问题
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.
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.)
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.
So my question is, how does one do a pre-publish step that finds and replaces that string contextually based on the configuration?
回答1:
You should take a look at Working with Multiple Environments.
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);
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
.
So by definition, when running your app in production it will automatically override appsettings.json
values with appsettings.Production.json
.
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.
来源:https://stackoverflow.com/questions/34321241/how-to-rewrite-a-file-before-publish-based-on-build-configuration