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

Web API makes it in Orchard


Web API has been released publicly, and obviously Orchard now supports it with great help from Nicholas Mayne who made the first implementation. The question is what does it mean for Orchard to support Web API ? If you don't know what Web API is you can get some more information on the official website.
ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.
Integrating Web API in Orchard means being able to expose Web API services as modules, and also reuse existing Orchard's components within Web API controllers, using dependency injection. We also needed to integrate it with multi-tenancy in mind.

Creating a Web API Controller

A Web API controller will work in the same way as MVC ones do. It will be responsible for implementing specific actions, which themselves will handle the requests from the client. Creating a Web API controller in Orchard is not different than creating on in any other ASP.NET MVC application, just add the class to you project, preferably in a Controllers folder.
public class HelloController : ApiController  {

    public HelloController(
        IOrchardServices orchardServices) {
        Services = orchardServices;
        T = NullLocalizer.Instance;
    }

    public IOrchardServices Services { get; private set; }
    public Localizer T { get; set; }

    public string Get() {
        return "Hello, world!";
    } 
}
In this specific example we also inject some services that Orchard is exposing. Nothing different than a standard MVC controller in Orchard. To request this specific controller though there is a default route which is created automatically by Orchard.
/api/{Module_Name|Module_Id}/{Controller}/{id}
For isntance, if the earlier controller had been added to a module called Orchard.MyModule, the default url targetting this controller would be
/api/Orchard.MyModule/Hello
The {id} part of the route is optional, and will map to an id parameter of the controller's actions if any. Also, if you have defined a your module's manifest, then you should use it instead of the module id.

Registering a custom route

The default route pattern might now suit your needs. Like for MVC controllers Orchard exposes some extension points to declarer custom routes.
using System.Collections.Generic;
using Orchard.Mvc.Routes;
using Orchard.WebApi.Routes;

public class HttpRoutes : IHttpRouteProvider {

    public void GetRoutes(ICollection<RouteDescriptor> routes) {
        foreach (RouteDescriptor routeDescriptor in GetRoutes()) {
            routes.Add(routeDescriptor);
        }
    }

    public IEnumerable<RouteDescriptor> GetRoutes() {
        return new[] {
            new HttpRouteDescriptor {
                Name = "HelloApi",
                Priority = -10,
                RouteTemplate = "HelloApi",
                Defaults = new { 
                    area = "Orchard.MyModule", 
                    controller = "Hello" 
                },
            }
        };
    }
}
Using this custom route provider we an target the controller with this url:
/HelloApi

Conclusion

Web API should give you new opportunities to expose your Orchard websites to external applications or to add more dynamic behaviors within your module. An interesting scenario for this would be to expose the Content Manager as an external service using the [Queryable] attribute from OData. Read this blog post for more details.



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