1

我有这个控制器:

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 错误,PATCH 请求:

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

   Request Method: PATCH
  • 响应正文:

{ "error":{ "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());

我错过了什么?

4

2 回答 2

1

@yaniv

您似乎想使用内置的路由约定来使用复合键修补实体。但是,内置的路由约定不支持复合键。

您可以自定义您自己的路由约定(请参见此处)或仅使用属性路由

属性路由简单易用。您只需要在您的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
}

谢谢。

于 2015-11-02T02:36:56.873 回答
0

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

于 2015-11-02T00:02:16.613 回答