Viktor
Viktor

Reputation: 71

Proper routes for both CRUD and non-CRUD operations

Let's suppose I want to have standard CRUD operations in a controller:

api/values - IEnumerable Get() api/values/1 - Get(int id) api/values - Post([FromBody]string value) api/values/5 - Put(int id, [FromBody]string value) api/values/5 - Delete(int id)

But I want to have additional operations, something like:

api/values/someoperation1 api/values/searchbysomething ... etc.

Having a second route below this doesn't help

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}"
        );

Upvotes: 0

Views: 710

Answers (1)

Filip W
Filip W

Reputation: 27187

By default it is not possible to mix REST style routing and RPC style routing in a single controller.

There is an open issue for that on ASP.NET Web Stack's Codeplex - http://aspnetwebstack.codeplex.com/workitem/184.

The only reasonable solution for you would be to have 2 controllers for each resource - separate one for CRUD and separate one for non-CRUD (RPC) type of operations.

Upvotes: 0

Related Questions