OpenRasta returns a list through JsonDataContractCodec

Suppose I have a resource as shown below:

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

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 configurations like:

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 displays as expected with

{"Bar":"Baz"}

however, / foos is not displayed at all. The message "8- [2010-09-22 13: 39: 29Z] Information (0) No response codec was found. The response object is null or the response code is already set." I confirmed that the result is not null before returning. I also tried returning Foo [], but had the same error.

+3
source share
2 answers

Figured it out. I had to change 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();
            }
        }
    }
}
+3
source

FYI, :

ResourceSpace.Has.ResourcesOfType<Foo>()
    .AtUri("/foos/{id}")
    .HandledBy<FooHandler>()
    .AsJsonDataContract();
ResourceSpace.Has.ResourcesOfType<List<Foo>>()
    .WithoutUri
    .AsJsonDataContract();
0

Source: https://habr.com/ru/post/1766006/


All Articles