我正在使用.net Web API V1构建一些 Restful api。

我们正在尝试为Web API定义一些路由。我在定义“放置”和“补丁”的路线时遇到问题。

它们具有相同的URL,唯一不同的是HttpMethod。在HttpMethod中,不​​支持Patch http://msdn.microsoft.com/en-us/library/system.net.http.httpmethod(v=vs.118).aspx

config.Routes.MapHttpRoute(
    "UpdateCustomer",
    "api/customers/id/{id}",
    new {controller = "Customers", action = "UpdateCustomer"},
    new {id = @"\d+", httpMethod = new HttpMethodConstraint(HttpMethod.Put)}
);

config.Routes.MapHttpRoute(
    "PatchCustomer",
    "api/customers/id/{id}",
     new {controller = "Customers", action = "PatchCustomer"},
     new {id = @"\d+", httpMethod = new HttpMethodConstraint(HttpMethod.**Patch**)}
);

最佳答案

编辑:更新到最新版本的WebAPI当前5.2.7(https://www.nuget.org/packages/Microsoft.AspNet.WebApi/)

原版的:

如果您不能使用它:
new HttpMethod("PATCH")
请参见以下示例在Web API源代码中的使用

https://github.com/aspnetwebstack/aspnetwebstack/blob/master/src/System.Web.Http/HttpPatchAttribute.cs

07-26 04:58