I found a workaround for this. I tested this only with self-hosting.
Create a PrefixedRouteAttribute class that inherits from RouteAttribute
public class PrefixedRouteAttribute : RouteAttribute { public static string Prefix { get; set; } public PrefixedRouteAttribute(string path) : base(path) { SetPrefix(); } public PrefixedRouteAttribute(string path, string verbs) : base(path, verbs) { SetPrefix(); } private void SetPrefix() { if (!string.IsNullOrWhiteSpace(Prefix)) { this.Path = string.Format("/{0}{1}", Prefix, Path); } } }
When creating AppHost, you can set the prefix
PrefixedRouteAttribute.Prefix = "api";
Then, instead of using the [Route] attribute, use the [Route Prefix] attribute in your classes
[PrefixedRoute("/echo")] [PrefixedRoute("/echo/{Value*}")] public class Echo { [DataMember] public string Value { get; set; } }
This will work for queries
/api/echo /api/echo/1
It can be improved. I do not really like the way I need to set the prefix through the static property, but I could not come up with a more suitable approach in my setup. The principle of creating an overriding attribute seems sound, though, and this is an important part.
source share