asp.net-web-api – oData $count不能与web api 4中的EntitySetC
发布时间:2020-12-16 04:03:44 所属栏目:asp.Net 来源:网络整理
导读:我正在使用EntitySetController创建一个oData web api控制器,除了获取总记录数之外,一切运行良好. 控制器定义如下: public class MyODataController : EntitySetControllerEntity1,int where TEntity : class{ public override IQueryableEntity1 Get() { r
我正在使用EntitySetController创建一个oData web api控制器,除了获取总记录数之外,一切运行良好.
控制器定义如下: public class MyODataController : EntitySetController<Entity1,int> where TEntity : class { public override IQueryable<Entity1> Get() { return EntityDatabase.Get(); } } 当我打电话给我时: http://localhost:44789/oData/MyOData/$count 我收到错误: 解决方法
不幸的是,虽然Web API在未来的版本中应该是开箱即用,但它并不支持$count开箱即用.在此期间,您仍然可以通过定义这些类来添加支持:
public class CountODataRoutingConvention : EntitySetRoutingConvention { public override string SelectAction(ODataPath odataPath,HttpControllerContext controllerContext,ILookup<string,HttpActionDescriptor> actionMap) { if (controllerContext.Request.Method == HttpMethod.Get && odataPath.PathTemplate == "~/entityset/$count") { if (actionMap.Contains("GetCount")) { return "GetCount"; } } return null; } } public class CountODataPathHandler : DefaultODataPathHandler { protected override ODataPathSegment ParseAtEntityCollection(IEdmModel model,ODataPathSegment previous,IEdmType previousEdmType,string segment) { if (segment == "$count") { return new CountPathSegment(); } return base.ParseAtEntityCollection(model,previous,previousEdmType,segment); } } public class CountPathSegment : ODataPathSegment { public override string SegmentKind { get { return "$count"; } } public override IEdmType GetEdmType(IEdmType previousEdmType) { return EdmCoreModel.Instance.FindDeclaredType("Edm.Int32"); } public override IEdmEntitySet GetEntitySet(IEdmEntitySet previousEntitySet) { return previousEntitySet; } public override string ToString() { return "$count"; } } 在MapODataRoute中注册它们: IList<IODataRoutingConvention> routingConventions = ODataRoutingConventions.CreateDefault(); routingConventions.Insert(0,new CountODataRoutingConvention()); config.Routes.MapODataRoute("OData","odata",GetModel(),new CountODataPathHandler(),routingConventions); 在您的控制器中,添加此方法: public HttpResponseMessage GetCount(ODataQueryOptions<TEntity> queryOptions) { IQueryable<TEntity> queryResults = queryOptions.ApplyTo(Get()) as IQueryable<TEntity>; int count = queryResults.Count(); HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StringContent(count.ToString(),Encoding.UTF8,"text/plain"); return response; } 为了避免将GetCount()复制到每个控制器,您可以定义一个从定义GetCount的EntitySetController派生的基类. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-5 – Windows身份验证在ASP.NET MVC 5网络应用
- ASP.NET OnClientClick =“return false;”不起作用
- Asp.Net Web Api基于令牌的授权,没有OWIN和AspNet.Identity
- asp.net – 具有绝对URL的Asp .Net MVC RedirectToAction
- ASP.NET MVC+EF 项目架构搭建
- asp.net-mvc – Sitecore PageEditor随机显示Glass.Mapper的
- asp.net-mvc-3 – ViewBag在Extension Class中返回null
- 澄清ASP.NET生命周期事件的序列
- Codebehind中的ASP.NET下拉列表与ASPX页面中的相关联
- asp.net-mvc – 在ASP.NET MVC中保留非表单模型数据
推荐文章
站长推荐
- .NET Core技术研究-HttpContext访问的正确姿势
- asp.net – 将单元测试慢慢集成到项目中的步骤
- asp.net – 最佳实践:DataBound with loop vs R
- 使用ASP.NET窗体身份验证的WCF服务
- asp.net – Windows EventLog不能超过32766个字符
- asp.net – 静态方法中的HttpContext.Current Nu
- asp.net – ThreadPools vs Own Threads用于长时
- ASP.NET MVC SEO URL
- asp.net-mvc – 将我的网站重定向到mvc中的默认控
- 我的ASP.Net代码可以从sendgrid得到确认邮件已发
热点阅读