3

Using OpenRasta and given the following 2 handlers:

public class HelloWorldHandler1
{
    public string GetHelloWorld(string test, string test2)
    {
        return "Hello.World!";
    }
}

public class HelloWorldHandler2
{
    public string GetHelloWorld(string test)
    {
        return "Hello.World!";
    }
}

Configured as follows

ResourceSpace.Has.ResourcesOfType<Resource1>()
    .AtUri("/{test}/{test2}")
    .HandledBy<HelloWorldHandler1>();

ResourceSpace.Has.ResourcesOfType<Resource2>()
    .AtUri("/Hello/{test}")
    .HandledBy<HelloWorldHandler2>();

If I GET /first/example then HelloWorldHandler1.GetHelloWorld is called with parameters first and example as expected.

If I GET /Hello/example then HelloWorldHandler2.GetHelloWorld is called with parameter Hello when I would expect it to be called with example.

Workaround 1

If I reverse the order of the configuration:

ResourceSpace.Has.ResourcesOfType<Resource2>()
    .AtUri("/Hello/{test}")
    .HandledBy<HelloWorldHandler2>();

ResourceSpace.Has.ResourcesOfType<Resource1>()
    .AtUri("/{test}/{test2}")
    .HandledBy<HelloWorldHandler1>();

Then the behavior is as expected:

GET /first/example => HelloWorldHandler1.GetHelloWorld["first","example"]
GET /Hello/example => HelloWorldHandler2.GetHelloWorld["example"]

Workaround 2

If you change the name of parameter:

ResourceSpace.Has.ResourcesOfType<Resource2>()
    .AtUri("/Hello/{somethingelse}")
    .HandledBy<HelloWorldHandler2>();

public class HelloWorldHandler2
{
    public string GetHelloWorld(string somethingelse)
    {
        return "Hello.World!";
    }
}

Then the behavior is also as expected.

Question

  1. Is it wrong to try to configure URIs in this way? Where they are potentially ambiguous as to which handler should be called?
  2. Is this a bug in OpenRasta?
Iain
  • 10,433
  • 16
  • 56
  • 62

1 Answers1

2
  1. yes I'd say it's wrong and 2. no that is not a bug.

A request to /Hello/example matches the pattern /{test}/{test2}, so if that pattern is first registered it handles the request.

Avoid ambiguous URIs to avoid ambiguous results :)

Jorrit Reedijk
  • 598
  • 3
  • 11
  • 22
  • Thanks Jorrit - that makes sense. The only confusing part is that the pattern from one resource is being applied to another handler. The first registered pattern wins but the most specific handler seems to be always picked. – Iain Apr 12 '12 at 11:23
  • thats because handlers are registered for resources. Uris map to resources. So if you have ambiguous uris, theres no way for the resource to be found, and the handler will be wahtever. In your case, the same handler is registered for two resources with the same URI, so out of all the methods in the handler, the one closer to the URI is the one that gets selected. It's a big big no no to register two resources with the same uri templates, and in general its not a good idea to register the same handler for multiple resources. – SerialSeb May 31 '12 at 21:38