.net Core How to read the configuration file content

How to read the .net core configuration file .net core development?

.net Core configuration has been restructured with respect to the previous version relies on system.configuration asp.net and xml configuration, the new configuration model provides the ability to streamline, funny, support diversification retrieved and given key / value configuration.

The following shows how to get the configuration information:

1. Make sure appsetings.json configuration has been done, for example, under the json string:

"MongodbHost": {
  "Connection": "mongodb://127.0.0.1:27017",
  "DataBase": "TemplateDb",
  "Table": "CDATemplateInfo"
}

 

2. Define solid model:

public class MongodbHostOptions
{
  public string Connection { get; set; }
  public string DataBase { get; set; }
  public string Table { get; set; }
}

 

3. In the Startup ConfigureServices method of registration services

services.Configure<MongodbHostOptions>(Configuration.GetSection("MongodbHost"));

4. Dependency injection through the object instance constructor

public the HomeController ( IOptions < MongodbHostOptions > mongodbHostOptions)
{
  // request IOptions <SampleWebSettings> container provided from the service
  _mongodbHostOptions = mongodbHostOptions.Value;

}

5. Get Value value profile

ViewData["Connection"] = _mongodbHostOptions.Connection;
ViewData["DataBase"] = _mongodbHostOptions.DataBase;
ViewData["Table"] = _mongodbHostOptions.Table;

Guess you like

Origin www.cnblogs.com/niguang/p/11608445.html