简体   繁体   中英

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?

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)
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM