我对json配置文件中的连接字符串有疑问,
照常 :
我创建了一个类:applicationDbcontext
我做了一个DbSet {get; set;}
将其集成到控制器中
将其添加到服务
我已阅读并尝试插入相关问题中的所有建议:
asp.net core 2.0 - Value cannot be null. Parameter name: connectionString
但是他们都不跟我一起工作
这是我的实现,
希望有人能帮助我,谢谢
A_ DB上下文:
public class ApplicationDbContext:DbContext
{
public ApplicationDbContext(DbContextOptions options) : base(options)
{
Database.EnsureCreated();
}
public DbSet<Person> Persons { get; set; }
}
}
B_应用程序设置:config.json和config.development.json
{
"ConnectionString": {
"testConnection": "Server=(localdb)\\mssqllocaldb;Database=mvc_db;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
C_服务注入
启动 :
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.Json")
.AddJsonFile("appsettings.Development.Json", true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
配置服务:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options => {
options.UseSqlServer(Configuration.GetConnectionString("testConnection"));
});
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
D_注入控制器:
private readonly ApplicationDbContext _db;
public PersonController(ApplicationDbContext db)
{
_db = db;
}
IEnumerable<Person> People { get; set; }
public IActionResult Index()
{
People = _db.Persons.ToList();
return View(People);
}
我试图将整个connectionString插入configuration.GetConnectionString(“ Here”);中;
并将连接字符串的位置从上向下更改,反之亦然。
但是没有任何方法可以解决connectionString返回空值的问题。
任何帮助,请感谢
参考方案
对connectionString使用此结构
{
"ConnectionStrings": {
"DefaultConnection": "xxx"
}
}
检查您错过了s
的json文件,它应该是ConnectionStrings
并以这种简单的方式访问您的连接字符串
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
您的情况DefaultConnection
应该是testConnection
我们正在将ASP.NET Core 2.1与.NET Framework 4.6.2结合使用。我们有一个客户需要向我们的Web应用程序发送一个很大程度上嵌套的json结构。当他们进行此调用时,我们将输出以下日志并返回错误: 读取器的MaxDepth超过了32。路径“ super.long.path.to property”,第1行,位置42111。”我浏览了…
ASP.NET-如何更改JSON序列化的方式? - javascript我正在使用ASP.NET通过以下查询返回Json文件:public ActionResult getTransactionTotals(int itemID) { DBEntities db = new DBEntities(); var query = from trans in db.Transactions // Linq query removed …
Asp.Net:在服务器端还原DropDownList的客户端SelectedItem - c#因此,我的页面上有一个dropDownList,其中包含数百个项目。用户可以通过在文本框中键入一些文本来过滤此DDL。然后对DDL进行相应的过滤(所有不包含输入文本的项目都将通过JavaScript删除)。然后,用户选择他的项目并按下按钮。通常,这将导致错误,因为DDL已更改并且ASP验证了PostBack数据。但是,使用EnableEventValidat…
在ASP.NET Core 2.1中添加自定义TagHelpers - c#我严格按照ASP.NET Core文档进行操作,并花费了大量时间来拖曳堆栈溢出,试图实现简单的自定义TagHelper,但没有成功。任何人都可以就任何陷阱或已知错误提出建议吗?应用程序属性:AssemblyName: AmpWeb Target Framework .NET Core 2.1 NuGet软件包Microsoft.AspNetCore.All …
asp.net:treeview-显示文本框是否选中? - c#我需要向用户显示字符串列表。用户可以选择多个字符串。如果选择了一组特定的字符串,则每个字符串旁边都会出现一个文本框。我想做的是向用户显示一个TREEVIEW,每个节点都是字符串之一。他们将通过选中复选框来选择所需的每个字符串。问题:如果用户选择特定的复选框,则我需要一个文本框来显示用户的GET输入。问题:如何从用户使用树形视图获取字符串输入? 参考方案 Tr…