Web Api oData v4 $ ref 404或服务器错误 [英] Web Api oData v4 $ref 404 or server error

查看:89
本文介绍了Web Api oData v4 $ ref 404或服务器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am able to successfully GET the related items from either end of this relationship using the following:

  • http://localhost/api/Exercises(1)?$expand=Measurements
  • http://localhost:49540/api/Exercises(1)/Measurements

现在,我正在尝试管理它们之间的关系.我发现了一些我尝试过的示例,但没有任何效果. 此处,然后这里是另一个.

Now I am trying to manage the relationship between them. There are a few examples Ive found out there that Ive tried and none work. Heres one and heres 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; }
}

动作

以下是我尝试过的动作及其要求:

Actions

Here are the actions I have tried and their requests:

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

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

URL: 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);
    }

URL:不适用

结果:服务器错误: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})'.

此人具有相同的症状,并且能够Microsoft发布修复程序后解决此问题.我使用的是最新版本的Web API和oData,仍然有此问题.

This guy had the same symptoms and was able to resolve it after Microsoft released a fix. I am running with the latest versions of Web API and oData and still have this problem.

<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" />

推荐答案

和往常一样,在向SO发布问题几分钟后,我就遇到了几天困扰我的问题的答案.

As usual, minutes after posting a question to SO, the answer to the problem Im stuck on for days comes to me.

根据删除实体之间的关系 -web-api/odata-v4/entity-relations-in-odata-v4"rel =" nofollow>此文档,我的网址应如下所示:

According to the Deleting a Relationship Between Entities at the bottom of this documentation, my URL should look like this:

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);
}

文档错误.经过一番挖掘,我发现您的网址应如下所示:

The documentation is wrong. After some digging, I found your url should look like this:

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

这篇关于Web Api oData v4 $ ref 404或服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆