如何从Appsettings.json读取EnableQuery的值

时间:2018-06-19 07:47:19

标签: c# asp.net-core

我想从配置文件中读取页面大小。我试过延伸但无法找到解决方案。 这是我的代码。

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

1 个答案:

答案 0 :(得分:1)

在Startup类中尝试以下配置:

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
}