Best and Cheap Orchard Cloud Hosting – Building a WebAPI Route in Orchard

18Nov, 2014

Building a WebAPI Route in Orchard

A web API is the application programming interface (API) for both the web server and web browser.It gathers definitions, procedures and protocols to help the communication between different computer softwares. HTTP services for example, is built by ASP.NET Web API in order to reach a variety of clients such as smartphones, tablets. In addition it is an ideal platform for building RESTful applications on the .NET Framework.
Best and Cheap Orchard Cloud Hosting - Building a WebAPI Route in Orchard
Orchard CMS is a free, open source content management system that allows users to rapidly create web sites on the Microsoft ASP.NET MVC platform. It is built on a flexible extensibility framework that enables developers and customizers to provide additional functionality through modules and themes.
There are a number of differences between regular MVC controllers and WebAPI controllers that make the latter much more suitable to building APIs: they are REST-centric, they can negotiate the format of the answer, etc. Implementing WebAPI controllers in Orchard is really simple. First, you need a route, and that will be the subject of this post.
You can add your own routes, by implementing IHttpRouteProvider, like this one:
public class DeploymentApiRoutes : IHttpRouteProvider {

    public void GetRoutes(ICollection<RouteDescriptor> routes) {

        foreach (var routeDescriptor in GetRoutes()) {

            routes.Add(routeDescriptor);

        }

    }

    public IEnumerable<RouteDescriptor> GetRoutes() {

        return new[] {

            new HttpRouteDescriptor {

                Priority = 5,

                RouteTemplate = “api/deployment/{controller}/{action}/{id}”,

                Defaults = new {

                    area = “Orchard.ImportExport”,

                    id = RouteParameter.Optional

                }

            }

        };

    }

}

The interface looks very much like the regular IRouteProvider, except that it uses HttpRouteDescriptors instead of RouteDescriptors. In fact, the two APIs are so close that it’s usually a safe bet to just stick Http in front of a class name to find its WebAPI equivalent.
You usually won’t have to create your own routes however: Orchard will scan modules for WebAPI controllers and generate routes for each of them. This work is done in Orchard.WebApi.Routes.StandardExtensionHttpRouteProvider. As you may have guessed, the work done by this class is very similar to what another class does for regular controllers: StandardExtensionRouteProvider. The routes generated by the WebAPI route provider are of the form:
api/{module.name}/{controller}/{id}
Without the DeploymentApiRoutes above, the same actions would be under api/Orchard.ImportExport/{controller}/{id} instead of api/deployment/{controller}/{id}.

Best and Cheap Orchard Cloud Hosting

Orchard is a young content management system with bright future. If you are looking for the right Windows ASP.NET hosting that supports Orchard hosting provider, ASPHostPortal.com is the right choice for you. They take great pride in offering the best customer service available in the industry, that’s what their customers deserve. They’re available to their customers 24/7, and when you do get connected to one of our agents, you’ll be talking to a real expert who can help you right away. They do NOT outsource any of our support avenues.
error: Content is protected !!