ASP.NET MVC 自定义 json 配置文件

ASP.NET MVC custom json config file

我有一个 json 配置文件:

{
  "chat": {
    "host": "http://localhost:4555"
  }
}

我还创建了 class :

public class ChatConf
{
    public String host { get; set; }
}

在我的启动文件中,我这样做:

services.AddOptions();
services.Configure<ChatConf>(Configuration.GetSection("chat"));

问题是如何在我的主布局中获取主机的值?

谢谢。

您需要在控制器中包含 Microsoft.Extension.IOption 才能使用 IOption 集合。然后你可以通过将它添加到控制器的构造函数来访问你的class。

here很好解释