Adding Routes via the API
This recipe covers the ability to add routes without using the provided RouteAttribute
class. This technique might be needed if you have restrictions on your development environment or requirements that might prevent you from using RouteAttribute
. In this situation where it's not ideal or possible to use the ServiceStack C# client, a client such as RestSharp with data transfer objects might be possible alternative.
Note
Using the routing attributes gives you the advantages of more streamlined development when using the ServiceStack JsonClient and is the recommended way of managing your application's routes. The solution in this recipe is only intended for situations where this is not possible.
How to do It...
From the AppHost
class, access the Routes
property on the base ServiceStackHost
class to add routes, passing the request object's type and the path to be used:
public class AppHost : AppHostBase { public AppHost(): base("Adding Routes via AppHost", typeof(AppHost).Assembly) { } public override void Configure(Container container) { Routes.Add<GreeterRequest>("/greetings/{Name}"); Routes.Add<FarewellRequest>("/farewell/{Name}", "GET"); Routes.Add<HowAreYouRequest>("/howareyou/{Name}", ApplyTo.Get); Routes.Add<IntroRequest>("/introducing/{0}/and/{1}",ApplyTo.Get,request => request.FirstName,request => request.SecondName); Routes.Add<IntroRequest>("/introducing/{0}/and/{1}/otherway",ApplyTo.Get, request => request.SecondName, request => request.FirstName); Routes.AddFromAssembly(typeof(ImFromRequest).Assembly); } }
How it works…
Using this recipe, routes are registered directly with the ServiceStackHost
class using two methods.
Routes.Add<Type>(path)
: This is a single registration of a request type and a path, which will map the URL to the service associated with the request object, in this caseGreeterRequest
is associated with the URL/greetings/{Name}
. The{Name}
URL binds the value in the place of the path to theName
property of the request object.Routes.Add<Type>(path,"GET, POST, DELETE")
: This is another way to register the route with the same passing of property values, but restricting the request to just the verb methods specified. If the verb isn't specified, it won't be available. If you try to access a route that hasn't been registered with the verb being requested, ServiceStack will respond with a 404 status code and default page advising you that the route was not found.Routes.Add<Type>(path, ApplyTo.Get)
: This is the same as using a string of verbs to restrict the path, but can be useful when trying to avoid possible bugs from spelling mistakes in the list of verbs.Routes.Add<Type>(pathWithFormater, ApplyTo.Get, propertyExpressions)
: This is an extension method to use for ordered variables to be used map properties to values within the path. This can be useful if you register different mappings with different verb restrictions or when dealing with complex routes that may have different binding behavior. In the example shown, by addingotherway
to the end of the same route, we changed the binding behavior of the same service.Routes.AddFromAssembly(assembly)
: This is a method that requires the use ofRouteAttribute
to find request objects with routes within a specified assembly.- The add method also provides a way to register types that are only known at runtime with alternate methods such as
Routes.Add(myTypeInstance, path)
. This could be used in conjunction with standard .NET reflection methods such asmyObjectInstance.GetType()
, which can be used on any object.
There's more...
In versions of ServiceStack greater than 4.0.18.0, the GetRouteAttributes(Type type)
method is virtual, allowing it to be overridden. This can allow the ability to source an array of RouteAttribute
objects from custom locations or using custom logic.
For example, when the server's AppHost
class is starting up, this method could check some custom configuration or even call external web services to work out what routes should be used. To achieve this, simply override the GetRouteAttributes
method in a suitable place in your code.