gpt4 book ai didi

ASP.NET 核心 2 : Model bound complex types must not be abstract or value types and must have a parameterless constructor

转载 作者:行者123 更新时间:2023-12-04 23:51:51 26 4
gpt4 key购买 nike

我想使用 IOptions 通过 POCO 获取配置,但它会抛出错误消息“模型绑定(bind)的复杂类型不能是抽象类型或值类型,并且必须具有无参数构造函数”

DatabaseSettings.cs

public class DatabaseSettings
{
public string MongoDBServer { get; set; }
}

appsettings.json

"DatabaseSettings": {
"MongoDBServer": "localhost"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.Configure<DatabaseSettings>(Configuration.GetSection("DatabaseSettings"));
}

Controller

public IActionResult Create(IOptions<DatabaseSettings> options)
{
string test = options.Value.MongoDBServer;
return View();
}

我不知道怎么办。你有什么建议给我吗?谢谢!

最佳答案

我遇到了同样的问题。我找到了 this回答很有帮助。

请尝试将 [FromServices] 添加到 Controller 操作:

public IActionResult Create([FromServices] IOptions<DatabaseSettings> options)

关于ASP.NET 核心 2 : Model bound complex types must not be abstract or value types and must have a parameterless constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50029367/

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