gpt4 book ai didi

c# - ODataController 返回 HTTP 406 Not Acceptable

转载 作者:太空狗 更新时间:2023-10-29 20:47:22 27 4
gpt4 key购买 nike

我正在 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<MarvelCharacter>("MarvelCharacters");
config.MapODataServiceRoute(
routeName: "Marvel",
routePrefix: "dude",
model: builder.GetEdmModel());
}
}
}

还有我的 Controller (不完整,但你明白了):

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<MarvelCharacter> 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<MarvelCharacter>>(marvelCharacters);
}
}
}

最佳答案

原来这个问题的答案非常简单,但我能找到的任何文档都没有很好地涵盖。

我试图在 WebAPI 2.2 上实现 OData 3 端点。我正在学习几个不同的教程,一些是针对 OData 3 的,一些是针对 OData 4 的。

我在我的 WebApiConfig 中使用 OData 4 (System.Web.OData),在我的 Controller 中使用 OData 3 (System.Web.Http.OData)。事实证明,他们在一起玩得不好。

我决定在这里发布答案,以防其他人遇到类似问题。

为了增加一点值(value),而且由于我无论如何都要混合使用两者,我决定继续为版本 3 和版本 4 设置支持,方法是在我的 WebApiConfig 中为命名空间设置别名,然后创建版本化 Controller 。

WebApi配置:

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<MarvelCharacter>("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<MarvelCharacter>("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<MarvelCharacter> 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<MarvelCharacter>>(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<MarvelCharacter> 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<MarvelCharacter>>(marvelCharacters);
}
}
}

它可能不是最好的架构(我可能会创建一个库来整合 Controller 之间的类似代码),但我已经测试过并且可以通过 OData 3 和 OData 4 成功查询,所以我对它很满意现在。

关于c# - ODataController 返回 HTTP 406 Not Acceptable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29975653/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com