Get Content Items with WebApi

Topics: Core, Customizing Orchard, General, Writing modules, Русский (Russian)
Nov 25, 2012 at 1:34 PM

I am trying to build a webapi controller which will return a set of content Items

in all of the examples I noticed the use of contentManager which is passed to the constructor

But if my controller is accessed exeternasly as following 'api/controller/values'

where am I supposed to get the contentManager from?


namespace YIYWebApiProj.Controllers
    public class ValuesController : ApiController
        //private readonly IContentManager _contentManager;

        //public ValuesController(IContentManager contentManager) {
        //        _contentManager = contentManager;
        //    }

        // GET api/values
        public IEnumerable<string>  Get()
            return _contentManager
                   .Query(VersionOptions.Published, "Page")
                   .Select(c => c.Id.ToString());
            //return new string[] { "julia"};

Nov 25, 2012 at 6:02 PM

You cant return an IEnumerable as its not serializable.

Orchard will inject the content manager for you even if you hit it externally, so no need to know how it works, just know it does :)