2015-11-01 98 views
1

我有這樣的控制器:的Web API的OData 2 V4 PATCH返回404

using System.Web.Http; 
using System.Web.OData; 

public class InvRecipientAutoInvoiceController : ODataController 
    { 
     // GET: odata/InvRecipientAutoInvoice 
     [EnableQuery] 
     public IQueryable<Inv_RecipientAutoInvoice> GetInvRecipientAutoInvoice() 
     { 
      return db.Inv_RecipientAutoInvoice.Where(a=>a.CompanyNumber == CompanyNumber); 
     } 

    [AcceptVerbs("PATCH", "MERGE")] 
    public IHttpActionResult Patch([FromODataUri] int RecipientNumber , [FromODataUri] int RecipientType, Delta<Inv_RecipientAutoInvoice> patch) 
     { 
      // XXXX Some Update Code 
     } 
    } 

的GET作品,我得到的結果,甚至可以對其進行排序。 但是當我做修補請求,我得到404錯誤, 補丁請求:

請求URL:http://localhost:61240/odata/InvRecipientAutoInvoice(RecipientNumber%3D443%2CRecipientType%3D400)

Request Method: PATCH 
  • 響應體:

{「錯誤「:{ 」code「:」「,」message「:」沒有找到與請求URI匹配的HTTP資源 'http://localhost:61240/odata/InvRecipientAutoInvoice(RecipientNumber=443,RecipientType=400)'。「,」innererror「:{ 」message「:」在控制器'InvRecipientAutoInvoice'上找不到與 請求相匹配的操作。「,」type「:」「,」stacktrace「:」「 }} }

  • 請求正文:
{"InvoiceLine1Description":"32132"} 

我使用它在ASP.net web項目(不MVC),

寄存器是:

config.MapODataServiceRoute(
routeName: "ODataRoute", 
routePrefix: "odata", 
model: builder.GetEdmModel()); 

我失去了什麼?

回答

0

@yaniv

看來你要使用內置的路由公約與組合鍵修補實體。但是,the built-in routing conventions不支持組合鍵。

您可以自定義您自己的路由約定(請參閱here),也可以僅使用attribute routing

屬性路由簡單易用。您只需將ODataRouteAttribute放在您的補丁操作上,那麼它應該可以工作。

[AcceptVerbs("PATCH", "MERGE")] 
[ODateRoute("InvRecipientAutoInvoice(RecipientNumber={RecipientNumber},RecipientType={RecipientType})"] 
public IHttpActionResult Patch([FromODataUri] int RecipientNumber , [FromODataUri] int RecipientType, Delta<Inv_RecipientAutoInvoice> patch) 
{ 
    // XXXX Some Update Code 
} 

謝謝。

0

當您撥打電話時,請求的內容類型是什麼?它是應用程序/ json-patch + json嗎? (而不是應用程序/ JSON)