Search code examples
c#asp.neturl-routingasp.net-mvc-routing

Route-parameter contains parts of route


I have a route that looks like this:

/foo/{identifier}/bar/

I have configured the route like this:

httpConfiguration.Routes.MapHttpRoute("Foo1", "foo/{identifier}/bar/", new
{
    controller = "Foo",
    action = nameof(Foo.Bar)
});

I also have a second route configured which routes to the same controller but a different method:

httpConfiguration.Routes.MapHttpRoute("Foo2", "foo/{*identifier}", new
{
    controller = "Foo",
    identifier = RouteParameter.Optional
});

My controller looks like this:

[HttpPost]
[ScopeAuthorize(SomeScope)]
public HttpResponseMessage Bar(string identifier, [FromBody] SomeDto dto)
{
    Console.WriteLine(identifier);
}

If I send a POST-request to this route with parameter identifier = hello and some correct data in the body, I would expect the value of identifier to be hello. However, the value is hello/bar. What am I missing here?


Solution

  • The problem was the catch-all parameter in the second route. When I removed it, the routing works as expected.

    So, the route foo/{*identifier} became foo/{identifier}.