如何在.NET Core MVC项目中转换appsettings.json?

编程入门 行业动态 更新时间:2024-10-25 02:21:56
本文介绍了如何在.NET Core MVC项目中转换appsettings.json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已将其他json配置文件添加到我的项目中

I've added additional json config files to my project

appsettings.DEV.json appsettings.QA.json

并根据环境将它们加载到Startup函数中:

and loaded them in the Startup function based on the environment:

public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); ...

我了解如何更改环境:在项目属性中修改ASPNETCORE_ENVIRONMENT环境变量的值.但是,似乎无法根据配置指定不同的环境变量,下拉列表标记为"N/A"并被禁用.

And I understand how to change the environment: modify the value of the ASPNETCORE_ENVIRONMENT environment variable in project properties. However, there does not appear to be the ability to specify different environment variables depending on the configuration, the dropdownlist is labeled "N/A" and disabled.

我看到的唯一选择是手动更改环境变量值,以更改使用哪些设置.我敢肯定有一种方法可以自动执行此操作,否则您将如何使用CI? (除了使用脚本来更改环境变量外,还必须有一种更简单的方法.)

The only option I see is to manually change the environment variable value, to change which appsettings are used. I'm sure there is a way to do it automatically, or else how would you ever use CI? (other than using a script to change the environment variable, there has to be an easier way).

此处的目标是为DEV,QA和PROD这三种环境设置自动构建和持续集成. DEV和QA位于同一台计算机上,因此请进行设置不能选择手动指定环境的环境变量.

The goal here is to setup automated builds and continuous integration for three environments: DEV, QA, and PROD. DEV and QA are on the same machine, so setting the environment variable that specifies the environment manually is not an option.

推荐答案

我从Tsengs的答案中找到了一个解决方案,但希望在此进行描述以求清楚. 在另一个问题的答案中找到了解决方案,但是问题却大不相同(并且我也扩展了答案),所以我不认为该问题应被标记为重复.

I've found a solution from Tsengs answer but wish to describe it here for clarity. The solution is found in the answer to another question however the question is quite different (and I've also expanded upon the answer) so I do not believe this question should be marked as a duplicate.

答案是此处

解决方案是在每个IIS站点上为键ASPNETCORE_ENVIRONMENT

The solution is to setup different environment variable values on each IIS site for the key ASPNETCORE_ENVIRONMENT

要执行的步骤是:

  • 转到IIS中的应用程序,然后选择Configuration Editor.
  • 选择Configuration Editor
  • 在Section组合框中选择system.webServer/aspNetCore(RC2和RTM)或system.webServer/httpPlatform(RC1)
  • 在From组合框中选择Applicationhost.config ....
  • 单击enviromentVariables元素并打开编辑窗口.
  • 设置环境变量.
  • 关闭窗口,然后单击应用".
  • 完成
  • Go to your application in IIS and choose Configuration Editor.
  • Select Configuration Editor
  • Choose system.webServer/aspNetCore (RC2 and RTM) or system.webServer/httpPlatform (RC1) in Section combobox
  • Choose Applicationhost.config ... in From combobox.
  • Click on enviromentVariables element and open edit window.
  • Set your environment variables.
  • Close the window and click Apply.
  • Done
  • 或者,您可以修改您的applicationHost.config文件(通常位于C:\Windows\System32\inetsrv\config\applicationHost.config

    Alternatively, you can modify your applicationHost.config file (normally located at C:\Windows\System32\inetsrv\config\applicationHost.config

    并在根<Configuration>标记下添加以下条目,其中"my-iis-site"是您的IIS站点的名称.

    And add the following entry under the root <Configuration> tag, where "my-iis-site" is the name of your IIS site.

    <location path="my-iis-site"> <system.webServer> <aspNetCore> <environmentVariables> <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="DEV" /> </environmentVariables> </aspNetCore> </system.webServer> </location>

    更多推荐

    如何在.NET Core MVC项目中转换appsettings.json?

    本文发布于:2023-11-13 23:47:37,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1585644.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:项目   如何在   NET   MVC   Core

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!