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.

51 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/

  • Rite said

    Thanks meant for delivering these amazing content. http://bit.ly/2A612UY

  • alldaychemist safe said

    I like this website - its so usefull and helpfull. http://bit.ly/2As3HsR

  • Johny Jackson said

    Does your website have a contact page? I'm having
    a tough time locating it but, I'd like to shoot you an email.
    I've got some recommendations for your blog you might be interested in hearing.
    Either way, great website and I look forward to seeing
    it grow over time.

  • Lauren Conrad said

    Hey there! This is my first visit to your blog! We are
    a collection of volunteers and starting a new project in a community in the same niche.

    Your blog provided us valuable information to
    work on. You have done a marvellous job!

  • TamelaSmall said

    I have checked your website and i've found some duplicate content,
    that's why you don't rank high in google's search results, but there
    is a tool that can help you to create 100% unique content, search for: Boorfe's tips unlimited content

  • FallonChief said

    I have checked your site and i have found some duplicate content, that's why you
    don't rank high in google's search results, but there is a
    tool that can help you to create 100% unique articles, search for:
    Best article rewritwer Ercannou's essential tools

  • mezo said

    May I simply say what a comfort to discover somebody who really knows what they're discussing online.

    You definitely understand how to bring an issue to light
    and make it important. More and more people ought to read this and understand this side of the
    story. I can't believe you're not more popular because you certainly have
    the gift.

  • DanteJuicy said

    Hi. I see that you don't update your site too often. I know that
    writing posts is boring and time consuming. But did you know that there is
    a tool that allows you to create new articles
    using existing content (from article directories or other websites from your niche)?

    And it does it very well. The new posts are high quality and pass the copyscape test.
    Search in google and try: miftolo's tools

  • how to buy bitcoin with paypal said

    Why sebastienros.com loads so slow?

  • busty girls galleries said

    Nice blog here! Also your website loads up fast!
    What web host are you using? Can I get your affiliate
    link to your host? I wish my web site loaded up as quickly as
    yours lol

  • Capsa Susun said

    continuously і used to гead smallеr content wһich also clear thеiг motive, and that іs alѕo
    happening with tһiѕ piece of writing ԝhich
    Ι am reading ɑt this place.

  • RebekahBig said

    Hi. I have checked your sebastienros.com and i see you've got
    some duplicate content so probably it is the reason that you
    don't rank high in google. But you can fix this issue fast.

    There is a tool that creates articles like human, just search in google:
    miftolo's tools

  • Fannie said

    Right here is the right site for anybody who would like to understand this topic.
    You know so much its almost hard to argue with
    you (not that I really would want to…HaHa). You definitely
    put a new spin on a subject which has been discussed for many years.

    Wonderful stuff, just wonderful!

  • alitabattleangelfullmovie.com said

    Excellent blog here! Also your site loads up fast!

    What web host are you using? Can I get your affiliate link to your host?
    I wish my site loaded up as quickly as yours lol

  • Super Bowl Tickets said

    Great post.

  • superbowlonlinestream.com said

    Usually I don't learn post on blogs, however I would like to say that this write-up very pressured me to check out and do it!
    Your writing taste has been amazed me. Thanks,
    very great article.

  • https://superbowllivestream.info said

    I love your blog.. very nice colors & theme.
    Did you design this website yourself or did you hire someone
    to do it for you? Plz answer back as I'm looking to construct my own blog
    and would like to find out where u got this from. thank you

  • superbowl2019.live said

    Howdy! Quick question that's completely off topic.
    Do you know how to make your site mobile friendly?
    My blog looks weird when browsing from my apple
    iphone. I'm trying to find a template or plugin that might be able to fix this
    problem. If you have any recommendations, please share.
    Thank you!

  • mywebsite said

    It's a shame you don't have a donate button! I'd without a doubt donate to this superb blog!
    I suppose for now i'll settle for bookmarking and adding your RSS feed to my Google account.
    I look forward to new updates and will share this site with my Facebook group.
    Chat soon!

  • reviews said

    Тhеre's certаinly a gгeat deal to learn ɑbout this subject.
    I love aⅼl the ρoints you maԀе.

  • Buy Gynexin Online said

    When someone writes an paragraph he/she maintains the plan of a user in his/her
    mind that how a user can know it. So that's why this piece of
    writing is amazing. Thanks!

  • inhouse said

    Thhis іs very intereѕting, You aгe а vеry skilled blogger.
    Ι've joined yokur feed annd ⅼoοk forward to seeking
    more ⲟff уour magnificent post. Aⅼso, I һave shared у᧐ur web site in my social networks!

  • Captain Marvel Free said

    I couldn't refrain from commenting. Exceptionally well written! https://123fullmovie.de/captainmarvel/ https://captainmarvelfull.co https://filmhds.com/captainmarvel/

  • امازون عربي said

    Incredible ρoints. Solid arguments. ᛕeep up tһe ɡood spirit.

  • viagra online canada pharmacy said

    Іt is not mmy fіrst tikme tо pay a visit thіѕ site, i am visiting tһis site daill aand obtaіn fastidious іnformation from here every daу.


    http://viagra.bingapproved.club

  • minecraft said

    You are so awesome! I don't suppose I've read through anything like that
    before. So great to find somebody with a few unique thoughts on this topic.
    Really.. thank you for starting this up. This site is one thing that
    is required on the web, someone with a little originality!

  • Hazel said

    Hi it's me, I am also visiting this web site regularly, this site is
    in fact good and the users are truly sharing good thoughts.

  • minecraft said

    Hi there! Quick question that's totally off topic. Do you know how to make
    your site mobile friendly? My web site looks weird when browsing from my iphone
    4. I'm trying to find a theme or plugin that might be able to resolve this
    issue. If you have any suggestions, please share.
    Cheers!

  • minecraft said

    Today, while I was at work, my sister stole my apple
    ipad and tested to see if it can survive a thirty foot drop, just
    so she can be a youtube sensation. My iPad is now destroyed and she has 83 views.

    I know this is totally off topic but I had to share it with someone!

  • minecraft said

    I believe this is among the such a lot significant information for me.
    And i'm glad reading your article. However wanna
    remark on few normal things, The site style is wonderful, the articles is actually nice : D.
    Just right process, cheers

  • http://tinyurl.com said

    Its such as you learn my mind! You appear to
    understand so much approximately this, like you wrote the ebook in it or something.
    I feel that you just can do with some percent to drive the message house a bit,
    but instead of that, this is magnificent blog. A great read.
    I will certainly be back.

  • sarasota moving company said

    Hey there! I've been reading your web site for a while now
    and finally got the bravery to go ahead and give you a shout out from Dallas Tx!
    Just wanted to tell you keep up the good job!

  • tinyurl.com said

    Hello, i think that i noticed you visited my web site so i came to go back the choose?.I'm
    attempting to in finding issues to improve my
    site!I guess its adequate to make use of some of your ideas!!

  • minecraft said

    Definitely imagine that that you said. Your favorite justification appeared to
    be at the net the simplest factor to take note of. I say to you, I certainly get irked even as people think about
    issues that they plainly do not understand about.
    You controlled to hit the nail upon the top as neatly as defined out the whole thing with no
    need side-effects , people could take a signal. Will probably be again to get more.
    Thanks

  • one bedroom apartment said

    Woh I like your articles, bookmarked!

Add a Comment