简体   繁体   English

如何从Appsettings.json中读取EnableQuery的值

[英]How to read value from Appsettings.json for EnableQuery

I want to read the page size from configuration file. 我想从配置文件中读取页面大小。 I have tried to an extend but couldn't find a solution. 我试图进行扩展,但找不到解决方案。 This is my code. 这是我的代码。

[HttpGet]
[EnableQuery(PageSize = 3)]
public async Task<IEnumerable<users>> Getusers()
{
     try
     {
          var   users = await userServices.QueryAll();
     }
     catch (Exception ex)
     {
          throw ex
     }
}

Try the following configuration in your Startup class: 在您的启动类中尝试以下配置:

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"] 
        });
    });
}

You need to add the corresponding entry to your appsettings.json 您需要将相应的条目添加到您的appsettings.json中

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "PageSize": 10
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM