So I’m using WCF WebAPI Preview 6 within my application (Orchard CMS). I’m trying to register a route and I’m constantly receiving:
A route with the resolved virtual path ‘~/api/register’ has already
been added. Parameter name: virtualPath
with the following:
var webApiRoute = new WebApiRoute(routePrefix, serviceHostFactory, serviceType) { Constraints = new RouteValueDictionary(constraints) };
being where it’s breaking. I can make the route path anything and continue to receive the same error.
My full method looks like the following:
public class WebApiRoutes : IRouteProvider
{
private readonly IRegistrationWebApiConfiguration _registrationWebApiConfiguration;
public WebApiRoutes(IRegistrationWebApiConfiguration registrationWebApiConfiguration)
{
_registrationWebApiConfiguration = registrationWebApiConfiguration;
}
public IEnumerable<RouteDescriptor> GetRoutes() {
return new[] {
new RouteDescriptor { Priority = 1, Route = MapServiceRoute(typeof(RegistrationResource), "api/register1006", _registrationWebApiConfiguration.GetInstance())
}
};
}
public void GetRoutes(ICollection<RouteDescriptor> routes) {
foreach (var routeDescriptor in GetRoutes())
routes.Add(routeDescriptor);
}
private static ServiceRoute MapServiceRoute(Type serviceType, string routePrefix, HttpConfiguration configuration = null, object constraints = null, bool useMethodPrefixForHttpMethod = true)
{
if (configuration == null)
configuration = new WebApiConfiguration(useMethodPrefixForHttpMethod);
var serviceHostFactory = new HttpServiceHostFactory { Configuration = configuration };
var webApiRoute = new WebApiRoute(routePrefix, serviceHostFactory, serviceType) { Constraints = new RouteValueDictionary(constraints) };
return webApiRoute;
}
}
UPDATE
So based on Piotr’s comments below, I tried his second bullet. The first did not work. The second one gives the same exception and crashes on the construct for my class:
public class MyRestRoute : WebApiRoute {
public MyRestRoute(string routePrefix, ServiceHostFactoryBase serviceHostFactory, Type serviceType) : base(routePrefix, serviceHostFactory, serviceType) {}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) {
return null;
}
}
That also happened with WCF
ServiceRoute‘s some time ago – you might this link helpful: http://orchard.codeplex.com/discussions/256924.You could also try:
RouteDescriptor.Priorityto a very high number (eg.Int32.MaxValue). This would ensure that your route would be registered before any other route, so it would not conflict with other, ‘normal’ routes.WebApiRoute, override theGetVirtualPathmethod and make it always return null. It will force the framework to omit that route when generating URLs.