不能将ODataQueryOptions与(常规 - 非webapi)Controller派生类一起使用?

时间:2013-07-09 19:21:56

标签: asp.net asp.net-mvc-4 odata

public class XYZController : Controller
    {            

        public ActionResult Index(ODataQueryOptions<Security> options = null)
        {
            var xyzs= GetXYZs().AsQueryable();
            var results = options == null ? xyzs: options.ApplyTo(xyzs);
            return View(xyzs);
        }
     }

这导致“没有为此对象定义无参数构造函数”错误。

我基本上想将odata兼容参数传递给常规控制器。

这不能做到吗?

2 个答案:

答案 0 :(得分:2)

我暂时(直到常规控制器可以使用ODataQueryOptions)通过使用Linq2Rest解决了这个问题(NuGet:install-package Linq2Rest)

这个非常强大的库允许我用一行代码完成我想要的东西:

using Linq2Rest;

 public ActionResult Index()
        {
            var filteredSource = GetXYZs().AsQueryable().Filter(Request.Params);

            return View(filteredSource);
        }

现在你可以像这样点击这个Controller的Index Action: xyz.com?$filter=something eq 'foo' and another gt 3&$orderby another

答案 1 :(得分:0)

ODataQueryOptions<T>现在仅支持Web API。也就是说,这是一个有趣的场景。我在codeplex上打开了这个issue来跟踪它。