ASP.NET Core 8 MVC连接字符串格式错误引发ArgumentException求助
ASP.NET Core 8 MVC连接字符串格式错误修复
问题描述
使用ASP.NET Core 8 MVC(版本8.0.302)时触发未处理异常:
ArgumentException: Format of the initialization string does not conform to specification starting at index 121
问题根源为连接字符串格式错误,当前appsettings.json配置如下:
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*", "ConnectionStrings": { "DefaultConnection": "Data Source=HP\\SQLEXPRESS;Initial Catalog=InvestmentProj;Integrated Security=True;Trust Server Certificate=True;MultipleActiveResultSets=true" } }
错误原因
错误指向索引121的位置,对应连接字符串中的Trust Server Certificate=True——SQL Server连接字符串的参数键名不允许包含空格,正确的键名应为TrustServerCertificate(无空格)。
修复方案
将连接字符串中的Trust Server Certificate=True替换为TrustServerCertificate=True,修复后的appsettings.json配置:
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*", "ConnectionStrings": { "DefaultConnection": "Data Source=HP\\SQLEXPRESS;Initial Catalog=InvestmentProj;Integrated Security=True;TrustServerCertificate=True;MultipleActiveResultSets=true" } }
验证步骤
- 检查连接字符串所有键值对,确保键名无空格、格式符合SQL Server连接字符串规范
- 可通过SQL Server Management Studio测试该连接字符串是否能正常连接数据库
- 启动项目验证异常是否消除
内容的提问来源于stack exchange,提问作者Amna A
相关产品推荐
相关产品推荐

