简体   繁体   English

WebApi OData内联计数不起作用

[英]WebApi OData inlinecount not working

I'm trying to make OData return the number of entities from the database when I pass it $inlinecount=allpages in the uri. 当我在uri $inlinecount=allpages传递给OData时,我试图使OData返回数据库中的实体数。 I have read in Stack Overflow that I should return IQueryable instead of IHttpActionResult , however that didn't solve the problem. 我已经在Stack Overflow中阅读到,我应该返回IQueryable而不是IHttpActionResult ,但这并不能解决问题。 I also tried to follow the tutorials on the asp.net website, but that also didn't give any results. 我也尝试遵循asp.net网站上的教程,但是也没有得到任何结果。

[EnableQuery(
        PageSize = BuildingConstants.BuildingsPerPage,
        MaxTop = BuildingConstants.MaxBuildingsPerPage,
        AllowedArithmeticOperators = AllowedArithmeticOperators.None,
        AllowedLogicalOperators = AllowedLogicalOperators.None,
        AllowedFunctions = AllowedFunctions.SubstringOf,
        AllowedQueryOptions = AllowedQueryOptions.Filter | AllowedQueryOptions.OrderBy | AllowedQueryOptions.Top | AllowedQueryOptions.Skip | AllowedQueryOptions.InlineCount)]
    [ResponseType(typeof(IEnumerable<ListedBuildingResponseModel>))]
    public IQueryable<ListedBuildingResponseModel> Get()
    {
        var buildings = this.buildings
            .GetBuildings()
            .ProjectTo<ListedBuildingResponseModel>();

        return buildings;
    }

This is everything I have written regarding OData. 这就是我写的有关OData的所有内容。 The controller inherits ApiController , not ODataController . 控制器继承ApiController而不是ODataController In the Register method I haven't added any OData routes or model builders. Register方法中,我没有添加任何OData路由或模型构建器。 $top , $skip , $orderby and everything else is working just fine, but $inlinecount=allpages . $top$skip$orderby以及其他所有东西都可以正常工作,但是$inlinecount=allpages Any suggestions on how to solve the problem? 关于如何解决问题有什么建议吗?

I managed to make count=true work by adding the following code: 我通过添加以下代码设法使count=true起作用:

 public static void Register(HttpConfiguration config)
    {
        config.MapODataServiceRoute("odata", "odata", model: GetEdmModel());
    }

    private static IEdmModel GetEdmModel()
    {
        var builder = new ODataConventionModelBuilder();
        builder.EntitySet<ListedBuildingResponseModel>("Buildings");

        builder.EnableLowerCamelCase();
        var edmModel = builder.GetEdmModel();
        return edmModel;
    }

Now everything is working fine except I can't use my other actions in the class, because I get 406 error status code and I don't know why. 现在一切正常,但是我不能在该类中使用其他操作,因为我得到406错误状态代码并且不知道为什么。 I don't want my other actions to support OData, so I am thinking for possible solutions. 我不希望我的其他操作支持OData,因此我正在考虑可能的解决方案。 Doing a separate controller only for the OData action is an option, but I have also households, expenses and debts controllers whose Get methods I want to support OData. 只为OData操作执行一个单独的控制器是一种选择,但是我也有一些家庭,支出和债务控制器,它们的Get方法要支持OData。 This means, I need to create 4 more controllers with only 1 Get method. 这意味着,我只需要使用1个Get方法就可以再创建4个控制器。 Is there a way I can create a single SearchController with methods GetBuildings , GetHouseholds , GetExpenses , and GetDebts that return different types? 有没有一种方法可以使用返回不同类型的方法GetBuildingsGetHouseholdsGetExpensesGetDebts创建单个SearchController Because, as far as I understand this line of code 因为据我了解,这行代码

uilder.EntitySet<ListedBuildingResponseModel>("Buildings");

somehow "binds" the BuildingsController to ListedBuildingResponseModel . 以某种方式将BuildingsController “绑定”到ListedBuildingResponseModel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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