Book Image

ServiceStack 4 Cookbook

Book Image

ServiceStack 4 Cookbook

Overview of this book

Table of Contents (18 chapters)
ServiceStack 4 Cookbook
About the Authors
About the Reviewers

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.


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<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);


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 case GreeterRequest is associated with the URL /greetings/{Name}. The {Name} URL binds the value in the place of the path to the Name 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 adding otherway 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 of RouteAttribute 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 as myObjectInstance.GetType(), which can be used on any object.

There's more...

In versions of ServiceStack greater than, 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.