我有一个Web API项目,我希望允许调用者在所有控制器上使用所有不同的查询选项。设置AllowedQueryOptions = AllowedQueryOptions.All适用于所有控制器
通过this thread和this thread启发我下面的代码添加到我的WebApiConfig.Register方法:
public static void Register(HttpConfiguration config)
{
//[already working configuration code]
//Allow for $format parameter to OData queries
config.Filters.Add(new EnableQueryAttribute()
{
AllowedQueryOptions = AllowedQueryOptions.All
});
}
的代码编译和运行,但是当我尝试在$格式参数添加到查询,我得到同样的异常如以前:
Query option 'Format' is not allowed. To allow it, set the 'AllowedQueryOptions' property on EnableQueryAttribute or QueryValidationSettings.
为什么不AllowedQueryOptions在WebApiConfig设置获得注册的所有控制器?
是的,我在操作上有[EnableQuery]属性,并且自从控制器生成以来一直存在,但它不起作用。 我在控制器方法中没有ODataQueryOptions。 –
有趣的是:如果我删除[EnableQuery],它的工作原理 - 是否有任何设置会改变[EnableQuery]的默认设置为不允许$格式? –
不,你可以看到它默认情况下允许,你的OData/WebAPI版本是什么? https://github.com/OData/WebApi/blob/master/OData/src/System.Web.OData/OData/Query/ODataValidationSettings.cs#L25 –