请注意,本站并不支持低于IE8的浏览器,为了获得最佳效果,请下载最新的浏览器,推荐下载 Chrome浏览器
欢迎光临。交流群:166852192

Building a WebAPI route in Orchard


There’s 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:
  DeploymentApiRoutes : IHttpRouteProvider {

      GetRoutes(ICollection<RouteDescriptor> routes) {
         (var routeDescriptor  GetRoutes()) {
            routes.Add(routeDescriptor);
        }
    }

     IEnumerable<RouteDescriptor> GetRoutes() {
         [] {
             HttpRouteDescriptor {
                Priority = 5,
                RouteTemplate = ,
                Defaults =  {
                    area = ,
                    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}.
In the next post, I’ll show how to write WebAPI actions to put behind these routes.



作者原创内容不容易,如果觉得内容不错,请点击右侧“打赏”,赏俩给作者花花,也算是对作者付出的肯定,也可以鼓励作者原创更多更好内容。
更多详情欢迎到QQ群 166852192 交流。