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; } } }