gpt4 book ai didi

c# - OData 路由异常

转载 作者:太空宇宙 更新时间:2023-11-03 12:50:21 26 4
gpt4 key购买 nike

我是新手,所以我将从代码开始,然后再进行解释。问题是这个

[HttpGet, ODataRoute("({key})")]
public SingleResult<Employee> GetByKey([FromODataUri] string key)
{
var result = EmployeesHolder.Employees.Where(id => id.Name == key).AsQueryable();
return SingleResult<Employee>.Create<Employee>(result);
}


[HttpGet, ODataRoute("({key})")]
public SingleResult<Employee> Get([FromODataUri] int key)
{
var result = EmployeesHolder.Employees.Where(id => id.Id == key).AsQueryable();
return SingleResult<Employee>.Create<Employee>(result);
}

我有这 2 个操作,但对于一个我想按字符串搜索另一个按数字搜索(尽管这不是问题)。如果我这样离开它,它将适用于 (int) 案例,但适用于字符串“....odata/Employees('someName')”,我将得到一个:HTTP 404(这是正常的)但如果我尝试使用字符串的方法更具体

webApiConfig 中的代码。

ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Employee>("Employees");
builder.Function("GetByKeyFromConfig").Returns<SingleResult<Employee>>().Parameter<string>("Key");

Controller 中的代码

[ODataRoutePrefix("Employees")]
public class FooController : ODataController
{

[HttpGet, ODataRoute("GetByKeyFromConfig(Key={key})")]
public SingleResult<Employee> GetByKey([FromODataUri] string key)
{ ... }
}

我得到了一个期望

{"The complex type 'System.Web.Http.SingleResult`1[[OData_Path.Employee, OData_Path, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' refers to the entity type 'OData_Path.Employee' through the property 'Queryable'."}

如果我在 WebApiConfig 中更改方法的返回类型

builder.Function("GetByKeyFromConfig").Returns<Employee>().Parameter<string>("Key");

我明白了,我不知道为什么。

{"The path template 'Employees/GetByKeyFromConfig(Key={key})' on the action 'GetByKey' in controller 'Foo' is not a valid OData path template. The request URI is not valid. Since the segment 'Employees' refers to a collection, this must be the last segment in the request URI or it must be followed by an function or action that can be bound to it otherwise all intermediate segments must refer to a single resource."}

我已经搜索并试图获得解释,但每次我找到答案都不起作用。我挣扎了好几天。


根据 2 个答案更新后

still have the Invalid OData path template exception

WebApi配置代码

 ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Employee>("Employees").EntityType.HasKey(p => p.Name);

var employeeType = builder.EntityType<Employee>();
employeeType.Collection.Function("GetByKey").Returns<Employee>().Parameter<int>("Key");



config.EnableUnqualifiedNameCall(unqualifiedNameCall: true);
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: null,
model: builder.GetEdmModel());

Controller 代码

  [EnableQuery, HttpGet, ODataRoute("Employees/GetByKey(Key={Key})")]
public SingleResult<Employee> GetByKey([FromODataUri] int Key)
{
var single = Employees.Where(n => n.Id == Key).AsQueryable();
return SingleResult<Employee>.Create<Employee>(single);
}

我也试过使用特定的命名空间

builder.Namespace = "NamespaceX";
[EnableQuery, HttpGet, ODataRoute("Employees/NamespaceX.GetByKey(Key={Key})")]

[EnableQuery, HttpGet, ODataRoute("Employees/NamespaceX.GetByKey")]

最佳答案

虽然您可以使用 OData 函数解决您的问题,但更简洁的解决方案是使用 alternate keys .作为Fan indicated , Web API OData 提供了一个 implementation of alternate keys这将允许您使用更直接的语法按姓名或编号请求 Employees:

GET /Employees(123) 
GET /Employees(Name='Fred')

您需要将以下代码添加到您的 OData 配置中。

using Microsoft.OData.Edm;
using Microsoft.OData.Edm.Library;

// config is an instance of HttpConfiguration
config.EnableAlternateKeys(true);

// builder is an instance of ODataConventionModelBuilder
var edmModel = builder.GetEdmModel() as EdmModel;
var employeeType = edmModel.FindDeclaredType(typeof(Employee).FullName) as IEdmEntityType;
var employeeNameProp = employeeType.FindProperty("Name");

edmModel.AddAlternateKeyAnnotation(employeeType, new Dictionary<string, IEdmProperty> { { "Name", employeeNameProp } });

确保在将模型传递给 config.MapODataServiceRoute 之前添加备用键注释。

在您的 Controller 中,添加一个方法来按名称检索 Employees(与您问题中的 GetByKey 方法非常相似)。

[HttpGet]
[ODataRoute("Employees(Name={name})")]
public IHttpActionResult GetEmployeeByName([FromODataUri] string name)
{
var result = EmployeesHolder.Employees.FirstOrDefault(e => e.Name == name);

if (result == null)
{
return this.NotFound();
}

return this.Ok(result);
}

关于c# - OData 路由异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35781552/

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