Search code examples
c#openrasta

OpenRasta return list via JsonDataContractCodec


Suppose I have a resource like below:

namespace OpenRastaApp.Resources
{
    public class Foo
    {
        public string Bar { get; set; }
    }
}

a handler like:

namespace OpenRastaApp.Handlers
{
    public class FooHandler
    {
        public object GetAll()
        {
            ArrayList foos = new ArrayList();
            foos.Add(new Foo() { Bar = "Hello," });
            foos.Add(new Foo() { Bar = " world!" });
            List<Foo> result = new List<Foo>(foos.ToArray(typeof(Foo)) as Foo[]);
            return result;
        }
        public object Get(int id)
        {
            return new Foo() { Bar = "Baz" };
        }
    }
}

and a configuration as:

namespace OpenRastaApp
{
    public class Configuration : IConfigurationSource
    {
        public void Configure()
        {
            using (OpenRastaConfiguration.Manual)
            {
                ResourceSpace.Has.ResourcesOfType<Foo>()
                    .AtUri("/foos")
                    .And.AtUri("/foos/{id}")
                    .HandledBy<FooHandler>()
                    .AsJsonDataContract();
            }
        }
    }
}

/foos/1 renders as expected with:

{"Bar":"Baz"}

however, /foos does not render at all. The debug console shows the message "8-[2010-09-22 13:39:29Z] Information(0) No response codec was searched for. The response entity is null or a response codec is already set." I've verified that result is non-null before returning. I've also tried returning a Foo[], but that had the same error.


Solution

  • Figured it out. Had to modify my configuration as follows:

    namespace OpenRastaApp
    {
        public class Configuration : IConfigurationSource
        {
            public void Configure()
            {
                using (OpenRastaConfiguration.Manual)
                {
                    ResourceSpace.Has.ResourcesOfType<List<Foo>>()
                        .AtUri("/foos")
                        .HandledBy<FooHandler>()
                        .AsJsonDataContract();
                    ResourceSpace.Has.ResourcesOfType<Foo>()
                        .AtUri("/foos/{id}")
                        .HandledBy<FooHandler>()
                        .AsJsonDataContract();
                }
            }
        }
    }