Routing matches route when it should not

Hello,

I was testing the wildcard routing option when I encountered a small issue (unrelated to wildcard): I have a route that has a few parameters in the url and the routing matches the request even if I do not give it the parameters.

For example, my request is defined with the route /routeWithOptParameters/{Name}/na/{Age} and if I hit it with the following url /routeWithOptParameters/Bob/na/28 I get the expected behavior. However if I only use the url /routeWithOptParameters my service is still called but with the default values for Name and Age.

I was surprised by this so I wanted to verify if I made a mistake or if this was the expected behavior. I tested with the latest version (4.0.60). Below is the code I used in a sample project.

    [Route("/routeWithOptParameters/{Name}/na/{Age}", Verbs = "Get")]
    [DataContract]
    public class RouteWithOptParameters : IReturn<string>
    {
        [DataMember]
        [ApiMember(IsRequired = true)]
        public string Name { get; set; }

        [DataMember]
        public int Age { get; set; }
    }

        public string Any(RouteWithOptParameters request)
        {
            return request.GetType().Name + ": Name (" + request.Name + ") / Age (" + request.Age + ") \n" + DateTime.Now.ToString();
        }

Do you have any additional configuration in your AppHost? like:

Routes.AddFromAssembly(typeof(FooService).Assembly)

Yes I do have an additional line in my AppHost:

    public class AppHost : AppHostBase
    {
        public AppHost()
            : base("ServiceStackWeb", typeof(MyServices).Assembly)
        { }

        public override void Configure(Container container)
        {
            Routes.AddFromAssembly(typeof(MyServices).Assembly);
        }
}

I can remove it easily since this was added for a test and never removed.

Out of curiosity, can you tell me why using AddFromAssembly causes the issue? Is it because I’m adding the same assembly that was defined in the constructor? I would have expected ServiceStack to ignore AddFromAssembly since the assembly was already specified in the constructor.

Because it’s used for automatically registering routes based on built-in conventions for each Service which is what’s adding your additional route. See the docs for how you can modify the auto route conventions.

That’s good to know.

Thanks