【问题标题】:How to read value from Appsettings.json for EnableQuery如何从 Appsettings.json 中读取 EnableQuery 的值
【发布时间】:2018-06-19 17:58:05
【问题描述】:

我想从配置文件中读取页面大小。我尝试过扩展但找不到解决方案。 这是我的代码。

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

【问题讨论】:

标签: c# asp.net-core


【解决方案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
}

【讨论】:

  • PageSize = Configuration["PageSize"] 这一行抱怨字符串和 int 之间的隐式转换。关于如何修复它的任何想法?
  • 尝试使用“PageSize”:“10”,不确定它是否会起作用,因为我没有收到相同的警告...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
  • 2017-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多