Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 在WebAPI应用程序中设置运行时的OData结果页大小_Asp.net_Asp.net Web Api2_Odata - Fatal编程技术网

Asp.net 在WebAPI应用程序中设置运行时的OData结果页大小

Asp.net 在WebAPI应用程序中设置运行时的OData结果页大小,asp.net,asp.net-web-api2,odata,Asp.net,Asp.net Web Api2,Odata,我正在使用ODataController编写一个ASP.NET Web API 2 Web服务。我已经了解了如何使用的属性设置页面大小。我希望允许web服务的用户在app.config中设置页面大小,然后让应用程序读取此设置并设置页面大小。问题是使用该属性需要将页面大小设置为编译时常量 属性的用法: [EnableQuery(PageSize = 10)] public IHttpActionResult GetProducts() { return repo.GetProducts()

我正在使用ODataController编写一个ASP.NET Web API 2 Web服务。我已经了解了如何使用的属性设置页面大小。我希望允许web服务的用户在app.config中设置页面大小,然后让应用程序读取此设置并设置页面大小。问题是使用该属性需要将页面大小设置为编译时常量

属性的用法:

[EnableQuery(PageSize = 10)]
public IHttpActionResult GetProducts()
{
    return repo.GetProducts();
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class ConfigurableEnableQueryAttribute : EnableQueryAttribute
{

    public ConfigurableEnableQueryAttribute()
    {
            var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
            var configuration = builder.Build();

            var configProps = configuration.GetSection("OData").GetChildren();
            var baseProps = typeof(EnableQueryAttribute).GetProperties();

            foreach (var configProp in configProps)
            {
                var baseProp = baseProps.FirstOrDefault(x => x.Name.Equals(configProp.Key));

                if (baseProp != null)
                {
                    baseProp.SetValue(this, Convert.ChangeType(configProp.Value, baseProp.PropertyType));
                }
            }
    }
}
我看到的一个建议的解决方案是构造EnableQueryAttribute,并在HTTPConfiguration配置对象上设置它,如下所示

int customSize = ReadPageSizeSettingFromConfigFile();

var attr = new EnableQueryAttribute { PageSize = customSize };
config.AddODataQueryFilter(attr);
但这实际上不起作用。HttpConfiguration的筛选器集合保持为空

另一篇帖子上的评论(隐藏在评论列表中)建议删除控制器上的所有EnableQuery属性,但这也没有效果。由于EnableQuery属性替换了旧属性,我想知道这是否是Microsoft的问题

这个问题以前被问过,但没有得到回答:


非常感谢您提供的所有帮助。

如果客户希望页面大小为10,并且需要第二页,您可以使用$top和$skip来实现您的目标:

localhost/odata/Customers?$top=10&$skip=10
关于动态设置页面大小:

public class MyEnableQueryAttribute : EnableQueryAttribute
{
    public override IQueryable ApplyQuery(IQueryable queryable, ODataQueryOptions queryOptions)
    {
        int pagesize = xxx;
        var result = queryOptions.ApplyTo(queryable, new ODataQuerySettings { PageSize = pagesize }); 
        return result;
    }
} 

并将此属性放入控制器方法。

如果客户端希望pagesize为10,并且想要第二页,则可以使用$top和$skip来实现目标:

localhost/odata/Customers?$top=10&$skip=10
关于动态设置页面大小:

public class MyEnableQueryAttribute : EnableQueryAttribute
{
    public override IQueryable ApplyQuery(IQueryable queryable, ODataQueryOptions queryOptions)
    {
        int pagesize = xxx;
        var result = queryOptions.ApplyTo(queryable, new ODataQuerySettings { PageSize = pagesize }); 
        return result;
    }
} 

并将此属性放入控制器方法。

您可以在webApiConfig重新注册方法中设置MaxTop的配置

public static class WebApiConfig{
      public static void Register(HttpConfiguration config){
          config.Select().Expand().Filter().OrderBy().MaxTop(100).count() // you can change max page size here
      }
}

您可以在webApiConfig重新注册方法中设置MaxTop的配置

public static class WebApiConfig{
      public static void Register(HttpConfiguration config){
          config.Select().Expand().Filter().OrderBy().MaxTop(100).count() // you can change max page size here
      }
}

我可以通过创建一个扩展EnableQuery属性的新属性(我称之为ConfigurableQueryAttribute)来实现这一点。在该属性的构造函数中,加载您的配置文件,并在基础中设置您感兴趣的任何设置。就我个人而言,我会循环使用appsettings的“OData”部分中提供的所有设置,如果EnableQuery属性中有匹配的设置,我会将它们转换为指定的类型并提供它们,但如果需要,您只能查找特定的设置

我的属性:

[EnableQuery(PageSize = 10)]
public IHttpActionResult GetProducts()
{
    return repo.GetProducts();
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class ConfigurableEnableQueryAttribute : EnableQueryAttribute
{

    public ConfigurableEnableQueryAttribute()
    {
            var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
            var configuration = builder.Build();

            var configProps = configuration.GetSection("OData").GetChildren();
            var baseProps = typeof(EnableQueryAttribute).GetProperties();

            foreach (var configProp in configProps)
            {
                var baseProp = baseProps.FirstOrDefault(x => x.Name.Equals(configProp.Key));

                if (baseProp != null)
                {
                    baseProp.SetValue(this, Convert.ChangeType(configProp.Value, baseProp.PropertyType));
                }
            }
    }
}
然后在控制器中

[HttpGet]
[ConfigurableEnableQuery]
public IQueryable<T> Get()
{
    return _context.Set<T>().AsQueryable();
}
[HttpGet]
[可配置查询]
公共IQueryable Get()
{
返回_context.Set().AsQueryable();
}

我可以通过创建一个扩展enable query属性的新属性(我称之为ConfigurableQueryAttribute)来实现这一点。在该属性的构造函数中,加载您的配置文件,并在基础中设置您感兴趣的任何设置。就我个人而言,我会循环使用appsettings的“OData”部分中提供的所有设置,如果EnableQuery属性中有匹配的设置,我会将它们转换为指定的类型并提供它们,但如果需要,您只能查找特定的设置

我的属性:

[EnableQuery(PageSize = 10)]
public IHttpActionResult GetProducts()
{
    return repo.GetProducts();
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class ConfigurableEnableQueryAttribute : EnableQueryAttribute
{

    public ConfigurableEnableQueryAttribute()
    {
            var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
            var configuration = builder.Build();

            var configProps = configuration.GetSection("OData").GetChildren();
            var baseProps = typeof(EnableQueryAttribute).GetProperties();

            foreach (var configProp in configProps)
            {
                var baseProp = baseProps.FirstOrDefault(x => x.Name.Equals(configProp.Key));

                if (baseProp != null)
                {
                    baseProp.SetValue(this, Convert.ChangeType(configProp.Value, baseProp.PropertyType));
                }
            }
    }
}
然后在控制器中

[HttpGet]
[ConfigurableEnableQuery]
public IQueryable<T> Get()
{
    return _context.Set<T>().AsQueryable();
}
[HttpGet]
[可配置查询]
公共IQueryable Get()
{
返回_context.Set().AsQueryable();
}

有点让人困惑。用户发出web api请求,您希望该api请求修改app.config文件吗?你确定这是个好主意吗?我已经扩展了我的问题来澄清。我想从app.config读取值,并在运行时设置它。有点混乱。用户发出web api请求,您希望该api请求修改app.config文件吗?你确定这是个好主意吗?我已经扩展了我的问题来澄清。我想从app.config中读取值并在运行时进行设置。我知道OData语法。我想限制(按页面大小)允许客户端从服务器请求的数据量。更新:如果被调用方未对查询应用任何筛选(例如$top、$filter等),则不会调用IQueryable ApplyQuery覆盖。我仍在寻找一个始终有效的解决方案。我知道OData语法。我想限制(按页面大小)允许客户端从服务器请求的数据量。更新:如果被调用方未对查询应用任何筛选(例如$top、$filter等),则不会调用IQueryable ApplyQuery覆盖。我仍在寻找一个永远有效的解决方案。