ODataController返回HTTP 406不可接受
我正在Web API 2.2上构建OData 3服务。
该服务正确返回我的实体的元数据,但在查询其中一个实际实体时返回406 Not Available
。 我做了很多研究(我目前正在学习几个教程),但我还没有找到任何实际工作的东西。
这是我的WebApiConfig:
using System; using System.Collections.Generic; using System.Linq; using System.Web.Http; using System.Web.OData.Builder; using System.Web.OData.Extensions; namespace MyProject { public static class WebApiConfig { public static void Register(HttpConfiguration config) { ODataModelBuilder builder = new ODataConventionModelBuilder(); builder.EntitySet("MarvelCharacters"); config.MapODataServiceRoute( routeName: "Marvel", routePrefix: "dude", model: builder.GetEdmModel()); } } }
和我的控制器(不完整,但你明白了):
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using System.Web.Http.OData; using System.Web.Http.OData.Query; using Microsoft.Data.OData; using MyProject; namespace MyProject.Controllers { public class MarvelCharactersController : ODataController { private static ODataValidationSettings _validationSettings = new ODataValidationSettings(); // GET: odata/MarvelCharacters public IHttpActionResult GetMarvelCharacters(ODataQueryOptions queryOptions) { // validate the query. try { queryOptions.Validate(_validationSettings); } catch (ODataException ex) { return BadRequest(ex.Message); } var entities = new myEntities(); var marvelCharacters = (from c in entities.MarvelCharacters select c).ToList(); return Ok<IEnumerable>(marvelCharacters); } } }
事实certificate这个问题的答案非常简单,但是我找不到任何文档都不能很好地解决这个问题。
我试图在WebAPI 2.2上实现OData 3端点。 我正在学习几个不同的教程,一些用于OData 3,一些用于OData 4。
我在我的WebApiConfig中使用OData 4( System.Web.OData
),在我的控制器中使用OData 3( System.Web.Http.OData
)。 事实certificate,他们并不能很好地融合在一起。
我决定在这里发布答案,以防其他人有类似的问题。
为了增加一点价值,因为无论如何我都在混合,我决定继续通过在我的WebApiConfig中对命名空间进行别名,然后创建版本控制器来设置对版本3和版本4的支持。
WebApiConfig:
using System; using System.Collections.Generic; using System.Linq; using System.Web.Http; using ODataV3 = System.Web.Http.OData; using ODataV4 = System.Web.OData; namespace MyProject { public static class WebApiConfig { public static void Register(HttpConfiguration config) { // OData V3 Route ODataV3.Builder.ODataModelBuilder builder3 = new ODataV3.Builder.ODataConventionModelBuilder(); builder3.EntitySet("MarvelCharactersV3"); // The MapODataRoute function is deprecated in WebAPI 2.2, // but I haven't found an alternative for supporting OData 3. config.Routes.MapODataRoute( routeName: "Marvel3", routePrefix: "dude3", model: builder3.GetEdmModel()); // ODate V4 Route ODataV4.Builder.ODataModelBuilder builder4 = new ODataV4.Builder.ODataConventionModelBuilder(); builder4.EntitySet ("MarvelCharactersV4"); ODataV4.Extensions.HttpConfigurationExtensions.MapODataServiceRoute( configuration: config, routeName: "Marvel4", routePrefix: "dude4", model: builder4.GetEdmModel()); } } }
MarvelCharactersV3(OData 3):
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using System.Web.Http.OData; // OData V3 using System.Web.Http.OData.Query; using Microsoft.Data.OData; using MyProject; namespace MyProject.Controllers { public class MarvelCharactersV3Controller : ODataController { private static ODataValidationSettings _validationSettings = new ODataValidationSettings(); // GET: odata/MarvelCharacters public IHttpActionResult GetMarvelCharactersV3(ODataQueryOptions queryOptions) { // validate the query. try { queryOptions.Validate(_validationSettings); } catch (ODataException ex) { return BadRequest(ex.Message); } var entities = new myEntities(); var marvelCharacters = (from c in entities.MarvelCharacters select c).ToList(); return Ok>(marvelCharacters); } } }
MarvelCharactersV4(OData 4):
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using System.Web.OData; // OData 4 using System.Web.OData.Query; using Microsoft.Data.OData; using MyProject; namespace MyProject.Controllers { public class MarvelCharactersV4Controller : ODataController { private static ODataValidationSettings _validationSettings = new ODataValidationSettings(); // GET: odata/MarvelCharacters public IHttpActionResult GetMarvelCharactersV4(ODataQueryOptions queryOptions) { // validate the query. try { queryOptions.Validate(_validationSettings); } catch (ODataException ex) { return BadRequest(ex.Message); } var entities = new myEntities(); var marvelCharacters = (from c in entities.MarvelCharacters select c).ToList(); return Ok>(marvelCharacters); } } }
它可能不是最好的架构(我可能会创建一个库来整合控制器之间的类似代码),但我已经测试过,我可以通过OData 3和OData 4成功查询,所以我现在对它很满意。
上述就是C#学习教程:ODataController返回HTTP 406不可接受分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/954826.html