简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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