WebAPI actions in Orchard

Building WebAPI controllers in Orchard is fairly simple: just inherit from System.Web.Http.ApiController. You’ll then be able to inject dependencies exactly in the same way that you would anywhere in Orchard. WebAPI is designed so that the default behavior is that a controller represents a category of resources, such as a product, an article, etc. There’s a bunch of conventions in place so that just naming the methods on the controllers is enough to wire them up. If this REST-like behavior is what you’re after, that’s great, just apply the conventions and you’re good to go. If you need to stray from that model, and implement something closer to what you’d do with a regular MVC controller, you’ll need to do a little more work.

First, you’ll need to make sure that there’s a route that’s able to point to your actions, or create one. The default routes for WebAPI in Orchard look like “api/{module.name}/{controller}/{id}”. Notice how there’s no action in that route. That’s because the system figures out what action to use based on the conventions I was talking about in the previous paragraph.

Outside of the conventions, you will need to have a proper route, and you’ll need to spell out to the system what verb the action should answer to:

[AcceptVerbs("POST")]
public HttpResponseMessage Content() {

And that’s pretty much it. There are a few more things to know about WebAPI, but they are not specific to Orchard…

No Comments