为了账号安全,请及时绑定邮箱和手机立即绑定

获取连接字符串时出错:ArgumentNullException:值不能为空。参数名称:连接字符串

获取连接字符串时出错:ArgumentNullException:值不能为空。参数名称:连接字符串

PHP
慕容森 2024-01-20 21:38:45
我正在使用 ASP.NET Core 2.0。下面是我的代码。启动:namespace foo{    public class Startup    {        public Startup(IConfiguration configuration)        {            Configuration = configuration;        }        public IConfiguration Configuration { get; }        // This method gets called by the runtime. Use this method to add services to the container.        public void ConfigureServices(IServiceCollection services)        {            // Add framework services.            services                .AddMvc()                .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());            services.AddDbContext<fooContext>(options => options.UseSqlServer(Configuration.GetConnectionString("UserDatabase")));        }        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.        public void Configure(IApplicationBuilder app, IHostingEnvironment env)        {            if (env.IsDevelopment())            {                app.UseBrowserLink();                app.UseDeveloperExceptionPage();            }            else            {                app.UseExceptionHandler("/Home/Error");            }            app.UseStaticFiles();            app.UseMvc(routes =>            {                routes.MapRoute(                    name: "default",                    template: "{controller=Home}/{action=Index}/{id?}");            });        }    }}应用程序设置.json:{  "Logging": {    "IncludeScopes": false,    "LogLevel": {      "Default": "Debug",      "System": "Information",      "Microsoft": "Information"    },    "ConnectionStrings": {      "UserDatabase": "Server=DESKTOP-FSES7UK;Database=xxx;User Id=sa; Password=xxxxxxx;Trusted_Connection=True;"    }  }}如何修复它?
查看完整描述

2 回答

?
慕森卡

TA贡献1806条经验 获得超8个赞

正如评论中提到的,尝试将连接字符串移至顶部(建议)修复方法是将键 * *置于键"ConnectionStrings"之外logging


应用程序设置.json


{

  "ConnectionStrings": {

      "UserDatabase": "Server=DESKTOP-FSES7UK;Database=xxx;User Id=sa; Password=xxxxxxx;Trusted_Connection=True;"

  },

  "Logging": {

    "IncludeScopes": false,

    "LogLevel": {

      "Default": "Debug",

      "System": "Information",

      "Microsoft": "Information"

  }

}


查看完整回答
反对 回复 2024-01-20
?
眼眸繁星

TA贡献1873条经验 获得超9个赞

问题是你的ConnectionStrings对象已经是对象的属性Logging。写下你的appsettings.json如下:


{

  "Logging": {

    "IncludeScopes": false,

    "LogLevel": {

      "Default": "Debug",

      "System": "Information",

      "Microsoft": "Information"

    },

  },

  "ConnectionStrings": {

      "UserDatabase": "Server=DESKTOP-FSES7UK;Database=xxx;User Id=sa; Password=xxxxxxx;Trusted_Connection=True;"

  }

}


查看完整回答
反对 回复 2024-01-20
  • 2 回答
  • 0 关注
  • 48 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信