.Net Core 配置文件appsettings

1.配置文件为appsettings

  在appsettings添加ConnectionStrings:

{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"ConnectionStrings": {
"MovieContext": "Server=(localdb)\\mssqllocaldb;Database=Movie-1;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}

  我们在构造函数里面初始化使用调用语句

using Microsoft.AspNetCore.Mvc.RazorPages;using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Http; namespace RazorPagesMovie.Pages
{
public class ceshiModel : PageModel
{
public string Message { get; set; }
private readonly string _connectionString;
public ceshiModel(IConfiguration configuration)//构造函数
{
_connectionString = configuration.GetSection("ConnectionStrings")["MovieContext"];
}
public void OnGet()//Get请求调用此函数
{
Message = _connectionString;
}
}
}
上一篇:AppSettings和ConnectionStrings的区别


下一篇:关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作