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.

16 Comments

  • Shane said

    I've been using Orchard for a few weeks, and whilst I found it initially a bit puzzling, I'm getting up to speed, and I'm really enjoying using it.

    What's more, it's posts like this that demonstrate that its pace of development is right up there, and there are exciting times ahead.

    Thanks for your work!

  • Ryan Keeter said

    Okay, so I have went through this process, after pulling down a recent build with HG, and cannot find "Orchard.WebApi.Routes" which makes me think that I don't have the correct pull of the source. I might have the correct source drop, but ReSharper doesn't recognize this namespace. What am I missing here? Am I just not on the correct drop? The source is from September 1, 2012.

  • siamak said

    I'v downloaded latest version of orchard.But there is no 'Orchard.WebApi' to use in routes.Any idea?

  • WebApiHelp said

    I followed your instructions but after adding the route, i was unable to access the controller through my browser.
    should I type :
    http://localhost:30320/api/HelloApi
    or
    http://localhost:30320/OrchardLocal/api/HelloApi
    ? because neither of them works.
    should this work in my browser?
    how can I access the controller through an external service?

  • WebApiHelp said

    I followed your instructions but after adding the route, i was unable to access the controller through my browser.
    should I type :
    http://localhost:30320/api/HelloApi
    or
    http://localhost:30320/OrchardLocal/api/HelloApi
    ? because neither of them works.
    should this work in my browser?
    how can I access the controller through an external service?

  • Fotis Pappas said

    Thanks for the tips Sebastien,
    here is a link of a module that includes the latest OData libraries and simplifies the process of returning OData objects from Orchard. The documentation describes how to serialize and return Blogparts.
    http://orchardodata.codeplex.com/

  • Summer said

    Excellent berat ! I wish to apprentice while you amend your website, how could i subscribe for
    a blog website? The account aided me a acceptable deal.
    I had been a little bit acquainted of this your broadcast provided bright clear
    concept

  • Phoenix Landscaping said

    At least once a year, they will require pruning if you want your area to look its best.
    By taking it in tiny steps, the design can become
    more manageable. To help pay for your project, you should divide
    it into different stages.

  • Bobby said

    In the novel, Abel exemplifies the idealistic libertarian who was among the instigators of a failed uprising against the president of
    Venezuela. Kearns landed an 18 inch rainbow, three
    20 inch bows and a 21 inch brown trout. Holding a $100 bill focus and saying, "the energy moves through me knowing I am an abundant being," for example is very liberating while
    making love.

  • Tim Kelley said

    Will you be able to implement an entire cms through the web api or just things like content?

  • Vinícius said

    What changes now in this 1.8 version?

  • www.fbhackingtools.com said

    Link exchange is nothing else except it is simply placing the other person's blog link on your page at proper place and other person will also do same for you.

  • clash of clans gem hack download free no survey said

    Aw, this was an extremely good post. Taking a few minutes and actual effort
    to produce a really good article… but what can I say… I hesitate a lot and don't manage to get anything
    done.

  • 4rx said

    Love the site-- extremely individual pleasant and
    lots to see! http://bit.ly/2p7LTKb

  • William said

    Thanks for sharing this post here. I am very glad to find this site.

  • Michael said

    You revealed it very well. https://bucketlist.org/idea/6LiG/ideas-to-assist-properly-deal-with-nervousness/

Add a Comment