【问题标题】:Web Api oData v4 $ref 404 or server errorWeb Api oData v4 $ref 404 或服务器错误
【发布时间】:2016-04-17 15:54:41
【问题描述】:

问题

我能够使用以下方法从该关系的任一端成功获取相关项目:

现在我正在尝试管理它们之间的关系。我发现了一些我尝试过的例子,但没有一个有效。 Heres oneheres another

型号

public class Exercise
{
    public int Id { get; set; }
    public virtual ICollection<Measurement> Measurements { get; set; }
}

public class Measurement
{
    public int Id { get; set; }
    public virtual ICollection<Exercise> Exercises { get; set; }
}

动作

以下是我尝试过的操作和他们的要求:

[HttpDelete]
public IHttpActionResult DeleteRef([FromODataUri] int key, [FromODataUri] string relatedKey, string navigationProperty)
{
   return StatusCode(HttpStatusCode.NoContent);
}

网址:http://localhost/api/Exercises(1)/Measurements$id=http://localhost/api/Measurements(4)

结果:404

[HttpDelete]
[ODataRoute("Exercises({key})/Measurements({relatedKey})/$ref")]
public IHttpActionResult DeleteMeasurementFromExercise(int key, int relatedKey)
{
   return StatusCode(HttpStatusCode.NoContent);
    }

网址:不适用

结果:服务器错误:The path template 'Exercises({key})/Measurements({relatedKey})/$ref' on the action 'DeleteMeasurementFromExercise' in controller 'Exercises' is not a valid OData path template. The URI segment '$ref' is invalid after the segment 'Measurements({relatedKey})'.

相关

This guy 有相同的症状,并且在 Microsoft 发布修复程序后能够解决它。我正在使用最新版本的 Web API 和 oData 运行,但仍然存在此问题。

<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net46" />
<package id="Microsoft.AspNet.OData" version="5.9.0" targetFramework="net46" />
<package id="Microsoft.OData.Core" version="6.15.0" targetFramework="net46" />
<package id="Microsoft.OData.Edm" version="6.15.0" targetFramework="net46" />

【问题讨论】:

    标签: c# asp.net asp.net-web-api odata


    【解决方案1】:

    像往常一样,在向 SO 发布问题几分钟后,我困扰了几天的问题的答案就来了。

    根据this documentation底部的删除实体之间的关系,我的网址应该是这样的:

    DELETE http://host/Suppliers(1)/Products/$ref?$id=http://host/Products(1)

    对于这样的操作:

    public IHttpActionResult DeleteRef([FromODataUri] int key, [FromODataUri] string relatedKey, string navigationProperty)
    {
        return StatusCode(HttpStatusCode.NoContent);
    }
    

    文档有误。经过一番挖掘,我发现你的网址应该是这样的:

    DELETE http://host/Suppliers(1)/Products(2)/$ref

    【讨论】:

      【解决方案2】:

      我痛苦地发现之前要解决此问题的另一件事是查看从您的全局发现 URL 检索到的实体元数据文件。它将包括关系两端的实体,以及多对多关系的任何连接表。这将为您提供用于每个对象的有效名称。

      MS 需要认真修复和改善 CDS 的用户体验,或者干脆放弃 Fing 的东西。十多年来,这一直是一场迟钝的噩梦。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多