簡體   English   中英

Web Api oData v4 $ref 404 或服務器錯誤

[英]Web Api oData v4 $ref 404 or server error

問題

我能夠使用以下方法從該關系的任一端成功獲取相關項目:

現在我正在努力管理他們之間的關系。 我發現了一些例子,我嘗試過但沒有任何效果。 繼承人一個繼承人另一個

型號

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})'. 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 運行,但仍然存在此問題。

<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 發布問題幾分鍾后,我已經解決了好幾天的問題的答案來了。

根據本文檔底部的刪除實體之間的關系,我的 URL 應如下所示:

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

我痛苦地發現,要更早地解決此問題的另一件事是查看從全局發現 URL 中檢索到的實體元數據文件。 它將包含關系兩端的兩個實體,以及多對多關系的任何連接表。 這將為您提供用於每個對象的有效名稱。

MS 需要認真修復和改善 CDS 的用戶體驗,或者已經放棄 Fing 的東西。 十多年來,這一直是一場遲鈍的噩夢。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM