47

I am building some restful api using .net Web API V1.

We are trying to define some routing for the web api. I am having some issue in defining the route for 'Put' and 'patch'.

They have the same URL and the only different is in the HttpMethod. In the HttpMethod, there is no support for 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**)}
);
Bernard Vander Beken
  • 4,848
  • 5
  • 54
  • 76
Stay Foolish
  • 3,636
  • 3
  • 26
  • 30

1 Answers1

113

EDIT: Update to the latest version of WebAPI currently 5.2.7 (https://www.nuget.org/packages/Microsoft.AspNet.WebApi/)

ORIGINAL:

If you cannot use instead:

new HttpMethod("PATCH")

See the following example use in Web API source code

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

Yishai Galatzer
  • 8,791
  • 2
  • 32
  • 41
  • You should really move to at least Web API 2.23 at this point (if you can) – Yishai Galatzer Apr 21 '17 at 00:31
  • Link broken. PATCH has been added as a property in .NET Core starting at 2.1 but still does not appear to be available as a property in Standard nor Framework. [https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpmethod.patch?view=netcore-2.2#System_Net_Http_HttpMethod_Patch] – joynoele Jan 04 '19 at 16:08
  • Elsa - Why are you still using WebAPI 1.0? This is now ancient software :). Updated the broken link – Yishai Galatzer Jan 04 '19 at 23:47
  • 2
    Thank you for updating the link to latest version. WebAPi 1.0 documentation & questions are still relevant for working on legacy system - as you say above "if you can". – joynoele Jan 07 '19 at 16:10