Iain
Iain

Reputation: 10779

In OpenRasta, is it wrong to configure potentially ambiguous URIs?

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?

Upvotes: 3

Views: 160

Answers (1)

Jorrit Reedijk
Jorrit Reedijk

Reputation: 608

  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 :)

Upvotes: 2

Related Questions