gpt4 book ai didi

c# - ASP.Net Core 2.2 读取 appsettings.json

转载 作者:太空宇宙 更新时间:2023-11-03 20:48:44 26 4
gpt4 key购买 nike

ASP.NetCore 2.2、VisualStudio 2019

我正在尝试弄清楚如何将 web.config 文件转换为 appsettings.json,但我遗漏了一些东西。

我有一个 startup.cs 文件,看起来像(为简洁起见进行了编辑):

using System;
using MyCoolApp.Models.Commodities;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Rewrite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace MyCoolApp {
public class Startup {
public Startup(IHostingEnvironment env) {
var builder = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json", true, true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true)
.AddEnvironmentVariables();

Configuration = builder.Build();
}

private IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services) {
services.AddDistributedMemoryCache();

services.Configure<IISServerOptions>(
options => {
options.AutomaticAuthentication = false;
}
);

services.AddMvc().SetCompatibilityVersion(
CompatibilityVersion.Version_2_2
);

var foo = Configuration.GetConnectionString("CommoditiesContext");
services.AddDbContext<CommoditiesContext>(
options => options.UseSqlServer(
Configuration.GetConnectionString("CommoditiesContext")
)
);

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
...
}
}
}

注意 var foo = Configuration.GetConnectionString(...) 行。

我有一个 web.config 文件,看起来像(经过大量编辑):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="CommoditiesContext" connectionString="Server=mydb.company.com;Initial Catalog=Things;Integrated Security=True;providerName=System.Data.SqlClient;" />
</connectionStrings>
</configuration>

我已经把它变成了“顶级”appsettings.json 文件:

{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"connectionStrings": {
"CommoditiesContext": "Server=mydb.company.com;Initial Catalog=Things;Integrated Security=True;providerName=System.Data.SqlClient;"
}
}

我有一个 appsettings.Development.json 文件,如下所示:

{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"location": {
"path": ".",
"inheritInChildApplications": "false",
"system.webServer": {
"handlers": [],
"aspNetCore": {
"processPath": "%LAUNCHER_PATH%",
"arguments": "%LAUNCHER_ARGS%",
"stdoutLogEnabled": "true",
"stdoutLogFile": ".\\logs\\stdout",
"hostingModel": "InProcess",
"environmentVariables": [
{
"name": "ASPNETCORE_HTTPS_PORT",
"value": "44375"
},
{
"name": "ASPNETCORE_ENVIRONMENT",
"value": "Development"
},
{
"name": "COMPLUS_ForceENC",
"value": "1"
}
],
"handlerSettings": [
{
"name": "debugFile",
"value": "aspnetcore-debug.log"
},
{
"name": "debugLevel",
"value": "FILE,TRACE"
}
]
},
"modules": [],
"isapiFilters": []
}
}
}

我也有一个 `Production one,但让我们暂时限制噪音。

我现在的问题是,当我在本地调试时,我可以在 startup.csservices.AddDbContext 行上设置一个 bp,然后查看值foo。它是 null。我要么错误地构建了 appsettings.json 文件,要么错误地读取了配置信息,但我不知道是哪一个。

我错过了什么?

编辑

program.cs

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace CalendarReservations {
public class Program {
public static void Main(string[] args) {
CreateWebHostBuilder(args).Build().Run();
}

private static IWebHostBuilder CreateWebHostBuilder(string[] args) {
return WebHost.CreateDefaultBuilder(args)
.UseIIS()
.UseStartup<Startup>();
}
}
}

最佳答案

问题是您在启动构造函数中为配置设置了错误的基本路径。 AppDomain.CurrentDomain.BaseDirectory 会给你错误的位置,因此不会加载任何配置文件。相反,请考虑改用 System.IO.Directory.GetCurrentDirectory()

关于c# - ASP.Net Core 2.2 读取 appsettings.json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57808731/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com