JustAMartin
JustAMartin

Reputation: 13733

Disable HTTP verb restrictions in ASP.NET WebAPI applications

I'm trying to add some RPC style methods to my WebAPI application, as described in this article:

REST vs. RPC in ASP.NET Web API? Who cares; it does both.

But it seems in the current version of WebAPI they have added some restrictions and now I'm forced to put those [HttpGet]&Co attributes on every method which doesn't have a name matching some HTTP verb. If I don't do that then I get 405 "Method not allowed".

Is there any simple solution to disable this behavior, so I can have controller methods with arbitrary names without HTTP verb attributes?

Upvotes: 1

Views: 1605

Answers (1)

Neil Thompson
Neil Thompson

Reputation: 6425

The names don't have to match the verb, but I think some verb information helps. This works for me:

[HttpGet]
public string ApiVersion()


[HttpPost]
public string SomethingElse(SomeObj myobj)

The WebApiConfig route is:

//http://encosia.com/rest-vs-rpc-in-asp-net-web-api-who-cares-it-does-both/
config.Routes.MapHttpRoute(
    name: "DefaultApiRpc",
    routeTemplate: "api/v{version}/rpc/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional } // for non-restful rpc calls ('cos they are still handy)
);

Upvotes: 1

Related Questions