我不记得在哪里看到过这个,但在为 .NET Core MVC 应用程序设置应用程序配置时,我遵循了博客上的建议。我创建了一个这样的模型来保存我的应用程序所需的一些设置:
public class BasePathSettings
{
public string BaseImageFolder { get; set; }
public string BaseApiUrl { get; set; }
}
我的初创公司有这个...
public void ConfigureServices(IServiceCollection services)
{
...
// this adds the base paths to container
services.Configure<BasePathSettings>(Configuration.GetSection("BasePathSettings"));
....
}
appsettings.json 中有这样的内容:
"BasePathSettings": {
"BaseImageFolder": "D:\\Images\\",
"BaseApiUrl": "http://localhost:50321/"
},
我像这样注入(inject)需要此信息的 Controller ......
private readonly BasePathSettings _settings;
public ClientsController(IOptions<BasePathSettings> settings)
{
_settings = settings.Value;
_client = new HttpClient();
_client.BaseAddress = new Uri(_settings.BaseApiUrl);
}
在我的本地主机上运行它,一切运行正常。
但是,当我将此应用程序部署到 Azure 时,我假设我需要在应用程序服务的常规设置中创建应用程序设置。因此,我创建了一个名为 BasePathSettings 的应用程序设置,并将该设置的 json 复制到值中:
{ "BaseImageFolder": "imagePath", "BaseApiUrl": "apiUrl" }
Azure 在ConfigureServices 代码中出现错误,声称web.config 在NTFS 中没有正确的权限。我猜真正的罪魁祸首是如何从 Azure 应用程序设置中读取 json 值。
我可以在那里使用 json 吗?如果是这样,是否需要采用不同的格式?
最佳答案
Can I even use json there? If so, does it need formatted differently?
要将分层结构设置添加到 Azure Web 应用程序,我们可以在部分名称和键名称之间放置一个冒号。例如,
use BasePathSettings:BaseImageFolder to set your folder
use BasePathSettings:BaseApiUrl to set your url
关于azure - 如何将 .NET Core 中的应用程序设置转换为 Azure 应用程序设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42826108/