Get Content Items with WebApi

Topics: Core, Customizing Orchard, General, Writing modules, Русский (Russian)
Nov 25, 2012 at 2: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")
                   .List()
                   .Select(c => c.Id.ToString());
            //return new string[] { "julia"};
        }
}

Developer
Nov 25, 2012 at 7: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 :)