ASP.NET Web API 2 allows to set route name on a controller:
Route names are useful for generating links, so that you can include a link in an HTTP response.
I define two controllers, each of them returns a link to another one in the response (it does not matter if that is a SSC ServicesApiController
or standard Web API ApiController
):
[Route("api/valuesA", Name = "ValuesA")]
public class ValuesAController : ServicesApiController
{
public object Get()
{
var link = Url.Link("ValuesB", new { });
return new { link_to_valuesB = link };
}
}
[Route("api/valuesB", Name = "ValuesB")]
public class ValuesBController : ServicesApiController
{
public object Get()
{
var link = Url.Link("ValuesA", new { });
return new { link_to_valuesA = link };
}
}
Request api/valuesA
returns link to valuesB
:
{
"link_to_valuesB": "http://mysite/api/valuesB"
}
The controllers work fine, but all the existing SSC controllers inherited from either EntityService<>
or ServiceBaseODataController<>
class just crash. All the SPEAK-based marketing applications heavily rely on the EntityService<>
and all of them just stop working returning numerous 500 Server Errors:
{"Message":"An error has occurred.","ExceptionMessage":"A route named 'ValuesA' is already in the route collection. Route names must be unique.\r\n
Parameter name: name","ExceptionType":"System.ArgumentException","StackTrace":" at System.Web.Routing.RouteCollection.Add(String name, RouteBase item)\r\n
at System.Web.Http.Routing.AttributeRoutingMapper.AddGenerationHooksForSubRoutes(HttpRouteCollection routeTable, IEnumerable`1 entries)\r\n
at System.Web.Http.Routing.AttributeRoutingMapper.<>c__DisplayClass2.<MapAttributeRoutes>b__0(HttpConfiguration config)\r\n
at System.Web.Http.HttpConfiguration.ApplyControllerSettings(HttpControllerSettings settings, HttpConfiguration configuration)\r\n
at System.Web.Http.Controllers.HttpControllerDescriptor.InvokeAttributesOnControllerType(HttpControllerDescriptor controllerDescriptor, Type type)\r\n
at System.Web.Http.Controllers.HttpControllerDescriptor..ctor(HttpConfiguration configuration, String controllerName, Type controllerType)\r\n
at System.Web.Http.Dispatcher.DefaultHttpControllerSelector.InitializeControllerInfoCache()\r\n
at System.Lazy`1.CreateValue()\r\n
--- End of stack trace from previous location where exception was thrown ---\r\n
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n
at System.Lazy`1.get_Value()\r\n
at System.Web.Http.Dispatcher.DefaultHttpControllerSelector.SelectController(HttpRequestMessage request)\r\n
at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"}
Any idea why is that? (Sitecore 8.2.3).
"ExceptionMessage":"A route named 'ValuesA' is already in the route collection.
.