C# 如何从Appsettings.json读取EnableQuery的值
我想从配置文件中读取页面大小。我试着延长时间,但找不到解决办法。 这是我的密码C# 如何从Appsettings.json读取EnableQuery的值,c#,asp.net-core,C#,Asp.net Core,我想从配置文件中读取页面大小。我试着延长时间,但找不到解决办法。 这是我的密码 [HttpGet] [EnableQuery(PageSize = 3)] public async Task<IEnumerable<users>> Getusers() { try { var users = await userServices.QueryAll(); } catch (Exception ex)
[HttpGet]
[EnableQuery(PageSize = 3)]
public async Task<IEnumerable<users>> Getusers()
{
try
{
var users = await userServices.QueryAll();
}
catch (Exception ex)
{
throw ex
}
}
[HttpGet]
[启用查询(页面大小=3)]
公共异步任务Getusers()
{
尝试
{
var users=await userServices.QueryAll();
}
捕获(例外情况除外)
{
投手
}
}
在启动类中尝试以下配置:
public Startup(IConfiguration configuration)
{
Configuration = configuration;
[...]
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(config => {
config.Filters.Add(new EnableQueryAttribute() {
PageSize = Configuration["PageSize"]
});
});
}
您需要将相应的条目添加到appsettings.json中
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"System": "Information",
"Microsoft": "Information"
}
},
"PageSize": 10
}
在启动类中尝试以下配置:
public Startup(IConfiguration configuration)
{
Configuration = configuration;
[...]
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(config => {
config.Filters.Add(new EnableQueryAttribute() {
PageSize = Configuration["PageSize"]
});
});
}
您需要将相应的条目添加到appsettings.json中
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"System": "Information",
"Microsoft": "Information"
}
},
"PageSize": 10
}
您可以从ASP.NET Core的配置、选项和依赖项注入的基础知识开始。您可以从ASP.NET Core的配置、选项和依赖项注入的基础知识开始。谢谢您,所以muchPageSize=configuration[“PageSize”]这一行抱怨字符串和int之间的隐式转换。你知道如何修复它吗?试试“PageSize”:“10”,不确定它是否能工作,因为我没有收到相同的警告…谢谢你,所以muchPageSize=Configuration[“PageSize”]这一行抱怨字符串和int之间的隐式转换。你知道如何修复它吗?试试“PageSize”:“10”,不确定它是否能工作,因为我没有收到相同的警告。。。