Web w=10=sh 2 Oda Wch PATCH w=11=sh 404

Web API 2 Odata V4 PATCH return 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 有效,我得到了结果,甚至可以对它们进行排序。 但是当我发出 PATCH 请求时,出现 404 错误, 补丁请求:

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

   Request Method: PATCH

{ "error":{ "code":"","message":"No HTTP resource was found that matches the request URI 'http://localhost:61240/odata/InvRecipientAutoInvoice(RecipientNumber=443,RecipientType=400)'.","innererror":{ "message":"No action was found on the controller 'InvRecipientAutoInvoice' that matches the request.","type":"","stacktrace":"" } } }

{"InvoiceLine1Description":"32132"}

我在 ASP.net 网络项目(不是 MVC)中使用它,

寄存器是:

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

我错过了什么?

当您拨打电话时,请求的内容类型是什么?是 application/json-patch+json 吗? (而不是 application/json )

@yaniv

您似乎想使用内置的路由约定来为带有 复合键 的实体打补丁。但是,the built-in routing conventions 不支持组合键。

您可以自定义自己的路由约定(参见 here ) or just use the attribute routing

属性路由简单易用。你只需要在你的 Patch 操作上放置一个 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
}

谢谢。