Web API 2 Odata V4 PATCH返回404

时间:2015-11-01 16:18:56

标签: odata asp.net-web-api2 odata-v4

我有这个控制器:

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'上找不到匹配的动作   请求“”类型“:””,‘堆栈跟踪’:‘’       }}

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

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

登记册是:

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

我错过了什么?

2 个答案:

答案 0 :(得分:1)

@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
}

感谢。

答案 1 :(得分:0)

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